note 64046 added to function.date

From: Date: Thu, 06 Apr 2006 11:46:31 +0000
Subject: note 64046 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-107430@lists.php.net to get a copy of this message
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. ---- Server IP: 203.220.0.234 Probable Submitter: 202.7.176.134 (proxied: 220.245.156.199) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/note/edit/64046 Del: integrated -- http://master.php.net/note/delete/64046/integrated Del: useless -- http://master.php.net/note/delete/64046/useless Del: bad code -- http://master.php.net/note/delete/64046/bad+code Del: spam -- http://master.php.net/note/delete/64046/spam Del: non-english -- http://master.php.net/note/delete/64046/non-english Del: in docs -- http://master.php.net/note/delete/64046/in+docs Del: other reasons-- http://master.php.net/note/delete/64046 Reject -- http://master.php.net/note/reject/64046 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#107430) next »