i want to compare two datetimes

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
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

i want to compare two datetimes

Post by lokesh_kumar_s »

hi i want to compare two datetimes how can i do it

ex:
compare(2009-06-24 17:15:00,today's date)
i.e., compare 2009-06-24 17:15:00 with todays date please specify how to do it.
in php
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: i want to compare two datetimes

Post by requinix »

How about you specify what it is you want to do. In complete, coherent sentences.
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Re: i want to compare two datetimes

Post by lokesh_kumar_s »

tasairis wrote:How about you specify what it is you want to do. In complete, coherent sentences.
is there any way to compare two datetime objects
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: i want to compare two datetimes

Post by requinix »

They aren't objects.

Compare how? The difference between the two? Measured how? Whether one is before the other? Are they in the same format?
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Re: i want to compare two datetimes

Post by lokesh_kumar_s »

tasairis wrote:They aren't objects.

Compare how? The difference between the two? Measured how? Whether one is before the other? Are they in the same format?
Compare how?
ans:which one is ancient(older)
Are they in the same format?
ans:yes they are in the same format
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: i want to compare two datetimes

Post by califdon »

Datetimes are simply numbers (of seconds since some base date), so you can compare them or operate on them just like any other numbers. To find the difference between 2 datetimes, just subtract them. Remember, they are in seconds, so you may want to divide them by 60 (minutes) or 60*60 (hours) or 60*60*24 (days), etc.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: i want to compare two datetimes

Post by Eric! »

Or in mysql:

Code: Select all

Select * From table where yourdate > '2009-07-01 12:00:00'
Gets everything after 2009-07-01 12:00:00.
lokesh_kumar_s
Forum Commoner
Posts: 48
Joined: Mon Apr 13, 2009 5:39 am
Contact:

Re: i want to compare two datetimes

Post by lokesh_kumar_s »

Eric! wrote:Or in mysql:

Code: Select all

Select * From table where yourdate > '2009-07-01 12:00:00'
Gets everything after 2009-07-01 12:00:00.
can i use compare oprators in php for comparing datetimes
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: i want to compare two datetimes

Post by Eric! »

lokesh_kumar_s wrote: can i use compare oprators in php for comparing datetimes
I would convert the string to a time stamp integer and compare them.

Code: Select all

$time = "2005-04-03 12:34:56";
$timestamp = strtotime($time);
http://php.net/manual/en/function.strtotime.php
Post Reply