Jump to content

[SOLVED] comparing date values


Vidya_tr

Recommended Posts

I have two dates in varaibles $fromdate and $todate

my $todate should be always greater than $fromdate

an if() condition checks for the valid dates...if they are the same an error message is produced.

suppose i have the values..

$fromdate="2009-05-03 09:00:00" and $todate="2009-05-03 09:00:00" (the values are taken from database and are of type DateTime.)

<?php
if($todate==$fromdate)
 {
 echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
 exit;}
?>

 

But here the error message is not produced....

Is there any problem in comparing the DateTime values????

Link to comment
Share on other sites

That code works correctly for me with those values.

 

Have you checked if $fromdate and $todate actually contain what you think they do? Is that piece of code even being executed? Is it inside of a conditional block of code that is being bypassed because of a FALSE value?

Link to comment
Share on other sites

my values are correct ..

Even if I put the code like this..

<?php
if($todate<=$fromdate)

{
 echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
 exit;}
?>

 

it checks the less than value and produces the message but not the equal to value...

Can I check this in some other way???

Link to comment
Share on other sites

<?php
I get the values of date by posting from another form
$fromdate=date('Y-m-d',strtotime($_POST['year1']."-".$_POST['month1']."-".$_POST['day1']));
$fromtime=strftime('%H:%M:%S',strtotime($_POST['hrs1'].":".$_POST['min1']));
$from=$fromdate." ".$fromtime;

$todate=date('Y-m-d',strtotime($_POST['year2']."-".$_POST['month2']."-".$_POST['day2']));
$totime=strftime('%H:%M:%S ',strtotime($_POST['hrs2'].":".$_POST['min2']));
$to=$todate." ".$totime;

/*Suppose I get the posted value as 
$fromdate=2009-05-03
$fromtime= 09:00:00
Then I have $from= 2009-05-03 09:00:00
(same values in $toddate ,$totime)*/

//function call to check valid date and time
checkDateTime($from,$to);
function checkDateTime($fromdate,$todate)
{
if($todate<$fromdate)
{ echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
 exit;}
 if($todate==$fromdate)
 { echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
 exit;}
}	

?>

 

(I am not retrieving the dates from database.sorry there was a mistake in what I told u before)

 

Link to comment
Share on other sites

<?php
   if($todate<$fromdate)
   { echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
    exit;}
    if($todate==$fromdate)
    { echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
    exit;}
?>

 

is the same as

 

<?php
   if($todate<$fromdate || $todate==$fromdate)
    { echo "You entered a Wrong date/time.Please enter the Date/Time correctly";
    exit;}
?>

 

Maybe you can use something like:

 

list($date, $time) = explode(' ', $_POST['fromdate']);

list($year, $month, $day) = explode('-', $date);

list($hour, $minute, $second) = explode(':', $time);

 

$fromdate = mktime($hour, $minute, $second, $month, $day, $year);

 

However these kind of dates aren't really practical especially not for the end-user try creating some regular expression if you can not create one then you may be able to find one on: http://regexlib.com/

Link to comment
Share on other sites

The reason your code does not work is because the format string in the following contains an extra space after the %S

 

$totime=strftime('%H:%M:%S ',strtotime($_POST['hrs2'].":".$_POST['min2']));

 

Had you done what Ken2k7 suggested in his post, you would have discovered this.

Link to comment
Share on other sites

besides what probably everyone forgot was that php can not compare dates only mysql can when you do this:

 

  if($todate<$fromdate) then you are comparing strings which means you are comparing there ascii value, example:

 

if ("A" > "a") 65 > 97 == false

 

so what you need to do is change the date into an integer and compare these therefor you can use my method and then

 

if ($from_unixtime < $to_unixtime) you will get the resulsts you need

Link to comment
Share on other sites

The posted code works, after the extra space in the formatting is removed.

 

If strings have the same format and the fields that make up the string are arranged from MSD (most significant digit - year) to LSD (least significant digit - second), then you can perform comparisons on them. This is why DATE and DATETIME values in a database are defined the way they are MSD to LSD.

Link to comment
Share on other sites

@PFMaBiSmAd That is very interesting i never though about it that way nevertheless i tested it and it didn't work, tried both:

 

<?php
if ("1-1-2009" > "31-12-2008") {
    echo "yep!";
} else {
    echo "nope!";
}
?>

 

<?php
if ("01-01-2009" > "31-12-2008") {
    echo "yep!";
} else {
    echo "nope!";
}
?>

 

Both say nope!

Link to comment
Share on other sites

The posted code works, after the extra space in the formatting is removed.

 

If strings have the same format and the fields that make up the string are arranged from MSD (most significant digit - year) to LSD (least significant digit - second), then you can perform comparisons on them. This is why DATE and DATETIME values in a database are defined the way they are MSD to LSD.

 

So you arrange a string from msd to lsd by converting it to an integer, correct?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.