Vidya_tr Posted May 2, 2009 Share Posted May 2, 2009 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???? Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824009 Share on other sites More sharing options...
Vidya_tr Posted May 2, 2009 Author Share Posted May 2, 2009 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??? Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824025 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Can you post the output of: var_dump($todate); and var_dump($fromdate); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824038 Share on other sites More sharing options...
Vidya_tr Posted May 2, 2009 Author Share Posted May 2, 2009 <?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) Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824081 Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 <?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/ Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824086 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824088 Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824089 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824095 Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 @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! Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824096 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 Read what I posted. They must be in the same format (each field must be the same length) and the fields must be from MSD to LSD. Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824099 Share on other sites More sharing options...
Vidya_tr Posted May 2, 2009 Author Share Posted May 2, 2009 yes It worked after the space was removed.. thanks for the help... Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824101 Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824105 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2009 Share Posted May 2, 2009 No. YYYY-MM-DD HH:MM:SS is MSD to LSD for a datetime, like I already stated - MSD (most significant digit - year) to LSD (least significant digit - second) Quote Link to comment https://forums.phpfreaks.com/topic/156483-solved-comparing-date-values/#findComment-824107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.