330 likes | 566 Views
Working with Date and Time. ISYS 475. How PHP processes date and time?. Traditional way: Timestamp Newer and object oriented way: DateTime class. What is a timestamp?.
E N D
Working with Date and Time ISYS 475
How PHP processes date and time? • Traditional way: • Timestamp • Newer and object oriented way: • DateTime class
What is a timestamp? • A timestamp uses an integer to represent a date and time. This integer store the number of seconds since midnight on 1/1/1970. This point in time is known as the Unix Epoch.
time() function • Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
date() function • With one argument: date($format) • Returns a string formatted according to the given format string using the current timestamp (which is the value of time()) • With two arguments: date($format, timestamp) • Returns a string formatted according to the given format string using the given integer timestamp
Example <?php echo date('n/j/Y',0) . "<br>"; $nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs echo 'Now: '. date('Y-m-d') ."<br>"; echo 'Next Week: '. date('Y-m-d', $nextWeek) ."<br>"; ?>
strtotime ( date/time string) • Returnng a timestamp using a date or date time string
Compute age given DOB $today=time(); $dob=strtotime('7/4/1990'); $age=($today-$dob)/(365*24*60*60); echo "You age is: $age";
PHP DateTime object • A class represents date and time with useful methods: • DateTime::add — Adds the specified DateInterval object to the specified DateTime object. • DateTime::setDate — Sets the date • DateTime::setTime — Sets the time • DateTime::setTimestamp — Sets the date and time based on an Unix timestamp • DateTime::setTimezone — Sets the time zone for the DateTime object • DateTime::sub — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object • DateTime::format ( string $format ) - Returns date formatted according to given format.
Creating a DateTime object • 1. A new DateTime object with current date: • $myDate=new DateTime(); • echo $myDate->format('n/j/Y'); • 2. A new DateTime object with a specified date: • $myDate=new DateTime('7/4/2013'); • echo $myDate->format('n/j/Y');
DateInterval class • Represents a date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTime's constructor supports. • Creating a new DateInterval object: • $myInterval = new DateInterval(‘interval specification');
An interval specification • The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.
DateTime class’ diff method • Returns the DateInterval object representing the difference between the two dates.
Compute Age $now=new DateTime(); $dob=new DateTime('7/4/1990'); $ageInterval= $now->diff($dob); // $ageInterval=$dob->diff($now); echo "You age is: " . $ageInterval->format(' %R%y years');