By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,050 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,050 IT Pros & Developers. It's quick & easy.

compare date

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
<?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

P: n/a
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

P: n/a
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

P: n/a
> 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.