note 64046 deleted from function.date by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:20:24 +0000 |
| Subject: | note 64046 deleted from function.date by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210763@lists.php.net to get a copy of this message | ||
Note Submitter: mel dot boyce at gmail dot com
----
I've been flicking through the comments looking for some succinct date code and have noticed an
alarming number of questions and over-burdened examples related to date mathematics. One of the most
useful skills you can utilize when performing date math is taking full advantage of the UNIX
timestamp. The UNIX timestamp was built for this kind of work.
An example of this relates to a comment made by james at bandit-dot-co-dot-en-zed. James was looking
for a way to calculate the number of days which have passed since a certain date. Rather than using
mktime() and a loop, James can subtract the current timestamp from the timestamp of the date in
question and divide that by the number of seconds in a day:
<?php
$days = floor((time() - strtotime("01-Jan-2006"))/86400);
print("$days days have passed.\n");
?>
Another usage could find itself in a class submitted by Kyle M Hall which aids in the creation of
timestamps from the recent past for use with MySQL. Rather than the looping and fine tuning of a
date, Kyle can use the raw UNIX timestamps (this is untested code):
<?php
$ago = 14; // days
$timestamp = time() - ($ago * 86400);
?>
Hopefully these two examples of "UNIX-style" timestamp usage will help those finding date
mathematics more elusive than it should be.