1 / 27

Working with Date and Time

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?.

gamada
Télécharger la présentation

Working with Date and Time

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Date and Time ISYS 475

  2. How PHP processes date and time? • Traditional way: • Timestamp • Newer and object oriented way: • DateTime class

  3. 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.

  4. time() function • Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

  5. 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

  6. 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>"; ?>

  7. strtotime ( date/time string) • Returnng a timestamp using a date or date time string

  8. Compute age given DOB $today=time(); $dob=strtotime('7/4/1990'); $age=($today-$dob)/(365*24*60*60); echo "You age is: $age";

  9. 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.

  10. 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');

  11. 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');

  12. 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.

  13. DateTime class’ diff method • Returns the DateInterval object representing the difference between the two dates.

  14. 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');

More Related