PHP/MySQL Date Question

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
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

PHP/MySQL Date Question

Post by aliasxneo »

Not sure which forum this belongs in since the issue relates both to PHP and MySQL.

I have a table in MySQL which uses the MySQL 'datetime' to hold dates. What I need to do is basically compare those dates to a Unix timestamp and determine if they are after the Unix timestamp I'm comparing too.

The problem is I'm not sure how to do this. Is there MySQL functions to convert Unix timestamps to datetimes from which I can then compare the two? Or do I need to somehow convert the Unix timestamp to that of a 'datetime' and then try to compare it like that?

Cheers,
Josh
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: PHP/MySQL Date Question

Post by alex.barylski »

Don't use MySQL DATETIME type...just use an integer and store the native timestamp makes comparison easier, faster and more portable.
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: PHP/MySQL Date Question

Post by manixrock »

you can use a mysql DATETIME field and the mysql UNIX_TIMESTAMP(date) function to get the unix timestamp.

More info here: http://dev.mysql.com/doc/refman/5.0/en/ ... -timestamp
gmapsuser
Forum Newbie
Posts: 5
Joined: Mon Jul 21, 2008 11:47 pm

Re: PHP/MySQL Date Question

Post by gmapsuser »

In MySql You can convert from DATETIME field to Unix Timestamp format using UNIX_TIMESTAMP() function or vice versa using FROM_UNIXTIME() function.

You can go through the following links for further references.

http://dev.mysql.com/doc/refman/5.0/en/ ... m-unixtime
http://dev.mysql.com/doc/refman/5.0/en/ ... -timestamp
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: PHP/MySQL Date Question

Post by Zoxive »

Hockey wrote:Don't use MySQL DATETIME type...just use an integer and store the native timestamp makes comparison easier, faster and more portable.
If you do that, then you will not be able to use the mysql date functions for the other queries using that table.
Post Reply