Page 1 of 1

transforming US date format into English date format, howto?

Posted: Wed Apr 14, 2004 11:49 am
by crazytopu
hi,

the following code is where i stucked. Maybe someoen could help.


The following snipet of code works just fine as long as I use US
Date formate i.e. m/d/y.

I have tried the whole day in order to find out how to find the difference
between two dates which are in English date formate i.e. d/m/y.

Code: Select all

<?php



<?php

$dateDiff = mktime(12,0,0,04,20,2003) - mktime(11,0,0,04,20,2003);
echo 'Difference in seconds: ' . $dateDiff . '<br />';

echo '<br />Years Difference   = '. floor($dateDiff/365/60/60/24);
echo '<br />Months Difference  = '. floor($dateDiff/60/60/24/7/4);
echo '<br />Weeks Difference   = '. floor($dateDiff/60/60/24/7);
echo '<br />Days Difference    = '. floor($dateDiff/60/60/24);
echo '<br />Hours Difference   = '. floor($dateDiff/60/60);
echo '<br />Minutes Difference = '. floor($dateDiff/60);

?>


?>
What should I modify? I tried this:

Code: Select all

$date1=15-04-04;
$date2=20-04-04;

$dateDiff = mktime(12,0,0,$date2) - mktime(11,0,0,$date1);
But I am aparently making a mistake. mktime function takes the date
value inside date1 and date2 as month and gives a result that is not
what i want. I wanted to show that the difference is 5 days.

I tried this one too

Code: Select all

$date3= date("d-m-Y", mktime(0,0,0,$date1) );
$date4=date("d-m-Y", mktime(0,0,0,$date2) );
but i am still unable to transform the american format into English
formate.

can anybody help?

Posted: Wed Apr 14, 2004 9:26 pm
by litebearer
Try this

Code: Select all

<?

// us date

// get the US date

$us_date = "09/10/2000";

// convert the US date into a timestamp

$us_timestamp = strtotime($us_date);

// convert the time stamp into English date format

$eng_date = date("d/m/Y", $us_timestamp);


echo "US Date ";
echo $us_date;
echo "<br>";
echo "English date ";
echo $eng_date;


?>

Posted: Wed Apr 14, 2004 11:32 pm
by litebearer
Well, this old man got intrigued and decided to write a little script for you that converts English Date format to US date format then calculates the difference between 2 English dates.

You will need to develop some error trapping to insure that the Englsih dates are infact valid dates.

Anyway, here it is....

Code: Select all

<?PHP
// Objective:  Calculate the difference between 2 dates, 
//                  the dates being in English date format
// notes:
//           this is a rough idea of how to accomplish the 
//           objective. Error trapping should be utilized 
//           to insure the English date is in the 
//           dd/mm/yyyy format and that it is a valid date

// create a function to convert English date to US date

function eng_2_us_date($eng_date_x) &#123;
     $delims = "/";
     $english_day = strtok($eng_date_x, $delims);
     $english_month = strtok($delims);
     $english_year = strtok($delims);
     $result = $english_month . "/" . $english_day . "/" . $english_year;
      return $result;
&#125;

$english_date_1 = "17/04/2001";
$english_date_2 = "22/04/2001";

$us_date_1 = eng_2_us_date($english_date_1);
$us_date_2 = eng_2_us_date($english_date_2);

// convert the US dates to timestamps

$us_stamp_1 = strtotime($us_date_1);
$us_stamp_2 = strtotime($us_date_2);

// calculate the difference between the dates

$diff_1 = $us_stamp_2 - $us_stamp_1;

// convert the difference to days
// difference / (60sec * 60min * 24hrs)

$diff_2 = $diff_1 / (60*60*24);
echo "The number of days difference is: ";
echo $diff_2;

?>
Hope this helps.

Lite...

Posted: Fri Apr 16, 2004 7:03 am
by crazytopu
Thanks litebearer

I finally made it work but your code looks better than that.