Math on dates?

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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Math on dates?

Post by uberpolak »

Is it possible for PHP to do math on dates? If so how? What I'm looking for is the ability to see how many days ago something was. As I have it now, I'm getting a date string in YYYY-MM-DD format, then subtracting the days, this only works up until there's a months difference. All I can come up with is to make a really big switch doing the appropriate subtraction depending on which month it was, before I do that though I'd like to know if there's a shortcut. I can't find anything in the manual.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

read

http://www.php.net/strtotime

http://www.devnetwork.net/forums/viewto ... =strtotime

http://www.devnetwork.net/forums/viewto ... =strtotime

strtotime will put your date in a time() format, which can easily have math done to it. then you can use date() to put it back in the format you want.
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

Thanks hob, I guess the real problem was that I didn't know where in the manual to look... Works great.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I avoid using mysql date columns when I can: I find it easier to work with timestamps stored in integer cols, then format the output with the date() function.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Personally I would use MySQL to do the work:
http://www.mysql.com/doc/en/Date_and_ti ... tions.html

Mac
Post Reply