jorntk@yahoo.co m wrote:[color=blue]
> Hi,
>
> How do i compare two date in YYYY-MM-DD format to determine which one is earlier?
>
> thanks and regards
>
> Jorn[/color]
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
jorntk@yahoo.co m wrote:
[color=blue]
> How do i compare two date in YYYY-MM-DD format to determine which one is
> earlier?[/color]
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
}
<?php
function compareDates($d ate1,$date2) {
$date1_array = explode("-",$date1);
$date2_array = explode("-",$date2);
$timestamp1 =
mktime(0,0,0,$d ate1_array[1],$date1_array[2],$date1_array[0]);
$timestamp2 =
mktime(0,0,0,$d ate2_array[1],$date2_array[2],$date2_array[0]);
if ($timestamp1>$t imestamp2) {
print "The second date is earlier than the first.";
} else if ($timestamp1<$t imestamp2) {
print "The first date is earlier than the second.";
} else {
print "The dates are equal.";
}
}
compareDates("2 004-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
<jorntk@yahoo.c om> wrote in message
news:e9da850b.0 404061824.7754e a10@posting.goo gle.com...[color=blue]
> Hi,
>
> How do i compare two date in YYYY-MM-DD format to determine which one is[/color]
earlier?[color=blue]
>
> thanks and regards
>
> Jorn[/color]
I noticed that Message-ID: <vYJcc.208563$p o.1043099@attbi _s52> from
kingofkolt contained the following:
[color=blue]
> $timestamp1 =
>mktime(0,0,0,$ date1_array[1],$date1_array[2],$date1_array[0]);[/color]
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/
Chris Hope <blackhole@elec trictoolbox.com > wrote in message news:<40736b8c_ 2@news.athenane ws.com>...[color=blue]
> jorntk@yahoo.co m wrote:
>[color=green]
> > How do i compare two date in YYYY-MM-DD format to determine which one is
> > earlier?[/color]
>
> 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
> }
>[/color]
FWIW, I added similar thoughts in the manual's usernotes section
sometimes ago <http://groups.google.c om/groups?selm=not e-33252%40php.net >
But, it was get deleted :-(
Comment