473,382 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 16218
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
1
by: vrparekh | last post by:
in my webapplication,user pick date in dd/mm/yy format, and i want to compare that date with another date using compare validator but compare validator compare the dates using mm/dd/yy format. ...
2
by: deepikashalini | last post by:
hi, Im storing Issue date in database. Issuedate stored as varchar. and then compare to Passing string date('03/06/2008') what is query to compare storing varchar date to passing string...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.