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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smidge
Forum Newbie
Posts: 1
Joined: Thu Mar 13, 2003 5:09 pm

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

Post 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:
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

date 3 days before

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply