Tuesday, June 12, 2007
I often forget how powerful the date object is in PHP and usually refer to the documentation to remind myself of how to format dates but one of the things I always forget to use is the strtotime() function. This function makes life a lot easier if say for example you need to know what the date was last saturday. This is how you do it:$lastSaturday = date('Y-m-d', strtotime('last Saturday'));
How easy is that?! Ok so what other keywords can I use you might ask?
this Saturday
first Saturday
second Saturday
third Saturday
fourth Saturday
last Saturday
next Saturday
But it doesn't stop there, you can also do:
-3 days last Saturday
+2 days Saturday
-2 weeks Saturday
+2 weels Saturday
Or maybe you just need to know:
yesterday
3 days ago
-3 days
tomorrow
+2 days
You get the picture...
Incredibly useful as you can imagine, for instance if you need to query a table based on how many entries from last monday to next sunday you just use the following variables:
$lastMonday = date('Y-m-d', strtotime('last Monday'));
$nextSunday = date('Y-m-d', strtotime('next Sunday'));
Jon 2:58 PM Permalink
