Page 1 of 1

Having a hard time with some date logic, please help :(

Posted: Thu Mar 13, 2003 5:09 pm
by Smidge
All I want to do is get a date 3 days before todays date, in a ymd format.

And for some reason i cant seem to do it lol, any help would be great :oops:

date 3 days before

Posted: Thu Mar 13, 2003 5:15 pm
by musashi

Code: Select all

$three_days_ago = date("ymd",mktime(1,1,1,date(m),date(d)-3,date(y)));
Now, I'm assuming that time of day (since I set it to 1:01) is not a concern. If you need to do time of day, you can adjust the mktime() function.

Here is the manual page: http://www.php.net/manual/en/function.mktime.php

Hope that helps

Posted: Thu Mar 13, 2003 5:16 pm
by volka
http://www.php.net/manual/en/function.date.php has some examples.
A simple approach is

Code: Select all

$threeDays = 3 * 86400 // 3 days, each having 60*60*24 seconds
$theDate = time() - $threeDays;
but I like

Code: Select all

$theDate = mktime (0,0,0,date("m") ,date("d")-3,date("Y"));
although you loose hours,minutes,... (can be fixed anyway).
If you're using a database to check some date/times check wether it has date-functions to perform this, e.g.

Code: Select all

SELECT Now()-Interval 3 day
is valid for mySql.