0

Duplicate

Dear All,

I have a PHP page where i wil be displaying some data from Mysql db. I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002) I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'

Can any one guide me

Thanks in advance

0

5 Answers 5

6

Use strtotime to convert the strings into a Unix timestamp, then use the date function to generate the correct output format.

Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:

$date1 = "28/04/2009";
$date2 = "28-04-2009";

function ukStrToTime($str) {
    return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}

$date1 = date($dateFormat, ukStrToTime($date1));
$date2 = date($dateFormat, ukStrToTime($date2));
Sign up to request clarification or add additional context in comments.

6 Comments

When i pass "28/04/2009 " to the above mentioned method, i am getting "01/01/70"
I hadn't noticed that you were using UK time. I've updated my example.
Its not "UK time", it is "not weird US date formats". Pretty much the rest of the world use dd/mm/yyyy see en.wikipedia.org/wiki/…. ;)
I would have thought PHP would use the locale setting for its default date order, as in setlocale(). It'd be disappointing if it didn't.
Oooh apparently there is an alternative to strtotime when you want locale taken into account: au2.php.net/manual/en/function.strptime.php
|
2

You should be all set with this:

echo date($dateFormat, strtotime($date1));
echo date($dateFormat, strtotime($date2));

Comments

1

You may want to look into the strptime function. This can convert any date from a string back into numeric values. Unlike strtotime, it can be adapted to different formats, including those from different locales, and its output is not a UNIX timestamp, so it's capable of parsing dates before 1970 and after 2037. It may be a little bit more work though because it returns an associative array though.

Unfortunately it's not available on Windows systems either so it's not portable.

Comments

0

If for some reason strtotime will not work for you, could always just replace the offending punctuation with str_replace.

function dateFormat($date) {
$newDate = str_replace(/, -, $date);
echo $newDate;
}

echo dateFormat($date1);
echo dateFormat($date2);

I know this will make most folks cringe, but it may help you with formatting non-date strings in the future.

1 Comment

str_replace expects strings, you're missing quotes.
0

rookie i am. so came up with the method that just do that. what mysql needs.. shish i used param 2... hope it helps. regards

public function dateConvert($date,$param){  
     if($param==1){  
         list($day,$month,$year)=split('[/.-]',$date);  
         $date="$year-$month-$day"; //changed this line  
         return $date;  
     }  
     if ($param == 2){ //output conversion  
            list($day,$month,$year) = split('[/.]', $date);  
            $date = "$year-$day-$month";  
            return $date;  
   }  
 }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.