Forum Moderators: coopster

Message Too Old, No Replies

How to reduce a date

         

zozzen

7:02 am on Feb 24, 2008 (gmt 0)

10+ Year Member



Hi,

I would like to reduce the result of date () but it doesn't seem to work.

The way I do is like this:
$day = date('M dS'); // show "Feb 24th"

In order to get, for example, "Feb 20th", then I tried:

echo date('M dS') - 4;

or
echo $day - 4;

but it can't show "Feb 20th".

Does anyone know how to do it? Thanks a lot!

cameraman

7:57 am on Feb 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



date() converts a timestamp to human formats. The optional second parameter is the timestamp to use, if not provided then it uses the system time. The easiest date fiddling function to use is strtotime() [php.net].

Since strtotime produces timestamps, you can combine the two, using strtotime to provide the timestamp:
date("M dS",strtotime('-4 days'));

zozzen

8:08 am on Feb 24, 2008 (gmt 0)

10+ Year Member



Thanks a lot, cameraman!

zozzen

8:41 am on Feb 24, 2008 (gmt 0)

10+ Year Member



And btw, i actually wanted to find out the "last saturday" in my program and i almost used a very inefficient way to do it (like switch or if...elseif). I know how to do it correctly! Very useful!

date('M dS',strtotime("last Saturday"));

cameraman

7:18 pm on Feb 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool!
Yep, strtotime() is practically magic.