469,356 Members | 2,033 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

compare date

Hi,

How do i compare two date in YYYY-MM-DD format to determine which one is earlier?

thanks and regards

Jorn
Jul 17 '05 #1
6 16044
jo****@yahoo.com wrote:
Hi,

How do i compare two date in YYYY-MM-DD format to determine which one is earlier?

thanks and regards

Jorn


You can use PHP's mktime() time fuction:

mktime ( int hour, int minute, int second, int month, int day, int year
[, int is_dst])

Simply split the date on dashes ("-") and enter the year, month, and day
in the respective places. It will return an integer value for the date,
or the time since epoch. Just compare the integers. For more
information, see: http://www.php.net/manual/en/function.mktime.php

Antoine
Jul 17 '05 #2
jo****@yahoo.com wrote:
How do i compare two date in YYYY-MM-DD format to determine which one is
earlier?


Because the order is year then month then day with leading zeros, you can
just do a straight string comparison on them.

if($date1 < $date 2) {
... date1 is earlier than date2 ...
}
elseif($date1 == $date2) {
... they are the same ...
}
else {
... date2 is earlier than date1
}

Chris

--
Chris Hope
The Electric Toolbox Ltd
http://www.electrictoolbox.com/
Jul 17 '05 #3
<?php
function compareDates($date1,$date2) {
$date1_array = explode("-",$date1);
$date2_array = explode("-",$date2);
$timestamp1 =
mktime(0,0,0,$date1_array[1],$date1_array[2],$date1_array[0]);
$timestamp2 =
mktime(0,0,0,$date2_array[1],$date2_array[2],$date2_array[0]);
if ($timestamp1>$timestamp2) {
print "The second date is earlier than the first.";
} else if ($timestamp1<$timestamp2) {
print "The first date is earlier than the second.";
} else {
print "The dates are equal.";
}
}
compareDates("2004-03-06","2004-05-06");
// outputs "The first date is earlier than the second."
?>

of course this doesnt test for the validity of the dates (for example if the
user inputs dates via a form and the dates are invalid, an error could
result)

- JP

<jo****@yahoo.com> wrote in message
news:e9**************************@posting.google.c om...
Hi,

How do i compare two date in YYYY-MM-DD format to determine which one is earlier?
thanks and regards

Jorn

Jul 17 '05 #4
I noticed that Message-ID: <vYJcc.208563$po.1043099@attbi_s52> from
kingofkolt contained the following:
$timestamp1 =
mktime(0,0,0,$date1_array[1],$date1_array[2],$date1_array[0]);


strtotime() is easier, but Chris's method is even easier still.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
Chris Hope <bl*******@electrictoolbox.com> wrote in message news:<40********@news.athenanews.com>...
jo****@yahoo.com wrote:
How do i compare two date in YYYY-MM-DD format to determine which one is
earlier?


Because the order is year then month then day with leading zeros, you can
just do a straight string comparison on them.

if($date1 < $date 2) {
... date1 is earlier than date2 ...
}
elseif($date1 == $date2) {
... they are the same ...
}
else {
... date2 is earlier than date1
}


FWIW, I added similar thoughts in the manual's usernotes section
sometimes ago <http://groups.google.com/groups?selm=note-33252%40php.net>
But, it was get deleted :-(

--
http://www.sendmetoindia.com - Send Me to India!
Email: rrjanbiah-at-Y!com
Jul 17 '05 #6
> strtotime() is easier, but Chris's method is even easier still

i had thought there was something to convert a string to a timestamp. i just
couldnt remember what it was lol, so i wasnt sure if it existed.

- JP
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by alexis | last post: by
4 posts views Thread by Richard Hollenbeck | last post: by
9 posts views Thread by Rich | last post: by
12 posts views Thread by Assimalyst | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.