110 likes | 279 Views
Date Manipulation. Unix Epoch..?. The easiest way to handle dates in PHP is using UNIX timestamps. A UNIX timestamp is the number of seconds since the UNIX Epoch . The Epoch is 1 st Jan 1970 00:00 GMT. Get current time. Use the time() function to get current or relative time. <?php
E N D
Unix Epoch..? • The easiest way to handle dates in PHP is using UNIX timestamps. • A UNIX timestamp is the number of seconds since the UNIX Epoch. • The Epoch is 1st Jan 1970 00:00 GMT.
Get current time • Use the time() function to get current or relative time. <?php $now = time(); $nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs ?>
Display a time.. • To display a time use the date() function along with a format string. <?php $nextWeek = time() + (7*24*60*60); echo‘Next week: ‘; echo date(‘d-m-Y’,$now).’<br />’; ?> Format strings: http://php.net/manual/en/function.date.php
String to timestamp • To convert a string to date, use strtotime() <?php echo strtotime("now"); echo strtotime("10 September 2000"); echo strtotime("+1 day"); echo strtotime("+1 week"); echo strtotime("next Thursday"); echo strtotime("last Monday"); ?>
String to timestamp • Note that strtotime() assume a US date format on string such as mm/dd/yyyy, so some modifications may be required.
What about dates before 1970? • Negative timestamps are not consistently supported in PHP. • Therefore we cannot use timestamps when using dates that might be before 1970.
The full information.. http://php.net/manual/en/ref.datetime.php We have looked at a sub-selection of this information. If you want to do something with dates.. This is the place to start looking.
Review • Know what an integer UNIX date is. • Can manipulate dates in PHP: creating, displaying, parsing from string data.