Jump to content

[SOLVED] Convert numerics to date for comparison purposes


Obarnes

Recommended Posts

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?

$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

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

 

 

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.

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.