Obarnes Posted March 4, 2007 Share Posted March 4, 2007 OK so I MUST be being completely stupid but here's my problem. I have two separate variables that include numeric string referring to a date in UK format (ddmmyyyy) as follows: $traveldate = 05042007 $orderdate = 03032007 I need to perform an action if $orderdate is more than 3 days from $traveldate, i.e. this is an 'earlybird' reward. How the heck do I convert the string to a date and then compare the two to determine what action to take? Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/ Share on other sites More sharing options...
ted_chou12 Posted March 4, 2007 Share Posted March 4, 2007 $date1 = explode("", $traveldate); $date1 = strtotime("".$date1[7]."".$date1[6]."".$date1[5]."".$date1[4]."-".$date1[3]."".$date1[2]."-".$date1[1]."".$date1[0].""); $date2 = explode("", $orderdate); $date2 = strtotime("".$date2[7]."".$date2[6]."".$date2[5]."".$date2[4]."-".$date2[3]."".$date2[2]."-".$date2[1]."".$date2[0].""); if (($date2 - $date1) > 259200) {echo "true, 3 more days";} else {echo "false, less than 3 days";} Ted Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199059 Share on other sites More sharing options...
Tyche Posted March 4, 2007 Share Posted March 4, 2007 Have a look at the mktime function which creates a unix timestamp based on a particular date int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] ) so you could use something like : $traveldate_timestamp = mktime (12,0,0,substr($traveldate,2,2),substr($traveldate,0,2),substr($traveldate,4,4)); $orderdate_timestamp = mktime (12,0,0,substr($orderdate,2,2),substr($orderdate,0,2),substr($orderdate,4,4)); Once the dates have been converted to a unix timestamp you test the difference between them EDIT : Or use ted_chou12's suggestion posted just before mine Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199061 Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 If you have PHP5+ <?php $trav = str_split($traveldate); $ord = str_split($orderdate); if(strtotime($trav[0]."/".$trav[1]."/".$trav[3]) - strtotime($ord[0]."/".$ord[1]."/".$ord[3]) > 3*24*60*60) echo "More than 3 days- Earlybird"; ?> If you got PHP4-3 <?php $trav = array($traveldate[0].$traveldate[1], $traveldate[2].$traveldate[3], $traveldate[6].$traveldate[7]); $ord = array($orderdate[0].$orderdate[1], $orderdate[2].$orderdate[3], $orderdate[6].$orderdate[7]); if(strtotime($trav[0]."/".$trav[1]."/".$trav[2]) - strtotime($ord[0]."/".$ord[1]."/".$ord[2]) > 3*24*60*60) echo "More than 3 days- Earlybird"; ?> Haven't tested it, but it should work Orio. Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199063 Share on other sites More sharing options...
Obarnes Posted March 4, 2007 Author Share Posted March 4, 2007 Firstly thank you all for being so quick. I started looking at Ted's script but it's giving me a false response regardless. Is this because I'm using the UK date format not US and it's causing a problem with the calculation? Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199082 Share on other sites More sharing options...
ted_chou12 Posted March 4, 2007 Share Posted March 4, 2007 I dont know actually, because I dont know if the inventor of php is british or not, but i am sure that it reads the US format under the pressure of microsoft. Ted Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199083 Share on other sites More sharing options...
Obarnes Posted March 4, 2007 Author Share Posted March 4, 2007 Yeah makes sense. Looks like I'll have to change my date output for this to work then. Thanks again for your help. Oliver. Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199084 Share on other sites More sharing options...
Obarnes Posted March 4, 2007 Author Share Posted March 4, 2007 In case anyone is following the above, I used the script below to convert a UK date to a US. $parseorderdate = "22/12/2007" list ($day, $month, $year) = split ("/", "$parseorderdate"); $usorderdate = $month."/".$day."/".$year; $userdate now = "12/22/2007" This allowed the above script by Orio to give the required result. For some reason I was getting a parse error on the explode command in Ted's but this could easily have been a syntax error on my part. Anyway all working now. Thanks again everyone. Link to comment https://forums.phpfreaks.com/topic/41101-solved-convert-numerics-to-date-for-comparison-purposes/#findComment-199129 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.