| Compare dates with PHP |
|
Compare dates with PHP I read a lot of people having trouble while comparing dates with php, trying to guess with some substring functions if date A is before or after date B. I would like to introduce some very convenient functions which simplify greatly your life. Let's begin with the definition of a timestamp : it's simply the number of seconds elapsed since the 1st january 1970. Yes, the number of seconds, it seems maybe a bit odd, but you discover then that these timestamps are very practical to manipulate: comparing dates, adding some days to a date, differences between dates, all these can be easily done with timestamps. Obviously, you think: and dates before 1970??? Depends of the platform on which runs PHP: on Windows, you cannot have negative timestamps, and it's the same problem with some linux distribution. You'll have to try to know, but if you get false, -1 or "1 jan 1970" as result of the function returning a timestamp with a correct date given, it's bad news. If it's ok, know you can normally handle dates between 13 december 1901 and 19 january 2038. strtotimeYou have in php some great functions to convert your human readable dates to timestamps. The first magic function is called strtotime (string to time) : give it your date, you get a UNIX timestamp! Let's see some examples: echo strtotime('2008-04-12 13:24'); echo strtotime('12 april 2008 13:24'); echo strtotime('12.04.2008 13:24'); And more powerfull, strtotime can recognize some keywords: echo strtotime('now'); echo strtotime('+4 days'); echo strtotime('+1 month'); echo strtotime('next monday'); echo strtotime('+2 weeks 3 days 4 hours 23 minutes'); The second argument of strtotime is a timestamp, and its default value is the actual timestamp (time()). So echo strtotime('+4 days') is relative to the current time. Of course you can also give strtotime your mysql date! (Note you can also use the mysql function UNIX_TIMESTAMP, which use a bit more ressources). To compare dates, it's now just a detail: // your first date coming from a mysql database (date fields) $dateA = '2008-03-01 13:34'; // your second date coming from a mysql database (date fields) $dateB = '2007-04-14 15:23'; if(strtotime($dateA) > strtotime($dateB)){ // bla bla } Better than substring, isn't it ?! Here is just another example, not relative to current date but to a particular date: strtotime('23 hours ago', strtotime('2005-04-13 14:00')); This mean 23 hours ago relatively to the second given date, which must be a timestamp. mktime()Another easy and useful function is the famous mktime(). It returns also a UNIX timestamp:int mktime(hour, minute, second, month, day, year); Every argument is optionnal, mktime() will return the current UNIX timestamp. Let's see an example: date('r', mktime(2, 24, 0, 3, 12, 2008); Be aware not to prefix the values with '0' (like '02'), PHP will treat it as an octal value (default PHP behaviour)! And what if you go over the number of days in the month for example? The function will correct the values for you: if you give it '03-33-2008', you will get the timestamp of the date '04-2-2008'! That's very interesting for us, because we don't have to care anymore about being in the range for the day, the month, ... A concrete example where we use this feature (inspired from php.net)
This code snippet will fill an array with all the date of the year 2008. See how the $nbDaysInYear variable varies: at first iteration, we have on line 4:
$yearArray[] = date("Y-m-d", mktime(0,0,0,1,365,2008);
No problem for mktime, it will find calculate what date in the year it is! ConclusionWith these function you should now be able to build all the basic functions you need for dates. Remember the basics: get a timestamp with strtotime or mktime, then convert it to the format you want with the date function. And remember you cannot handle dates beyond the limit dates given by the platform on which PHP runs.And about dates with mySQL ? see this article
Powered by !JoomlaComment 3.23
3.23 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |