0

I have a date field in html. From it I transmit date via POST in php to another file and different browsers transmit it in different formats. For example, chrome 2014-12-30, mozilla 30.12.2014. How I can easily change date format, when I assign this field to php variable?

user3828374
  • 99
  • 3
  • 11

5 Answers5

0

change with date() in php

$date = '30.12.2014';
echo date('Y-m-d', strtotime($date));

or datetime()

$datetime = new DateTime($date);
echo $datetime->format('Y-m-d'); 
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

If you use the DateTime class to create a date formatting the date is very easy.

$dateNow = new DateTime();

Then to format the date into the wanted format you use the format method

$dateNow->format('Y-m-d') // Google Chrome
$dateNow->format('d.m.Y') // Mozilla Firefox
JKaan
  • 359
  • 2
  • 14
0

Use date with strtotime. The first argument in date() is the format you desire, the second is the strtotime'd date you have pulled.

echo date('d m y', strtotime("date POST var here"));
user3821538
  • 117
  • 4
0

with the strftime function, you can get localized dates.

use it along with setlocale()

example from php.net:

setlocale(LC_TIME, "C");
echo strftime("%A");
setlocale(LC_TIME, "fi_FI");
echo strftime(" in Finnish is %A,");
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %A and");
setlocale(LC_TIME, "de_DE");
echo strftime(" in German %A.\n");
Raphael Müller
  • 2,180
  • 2
  • 15
  • 20
0
<?php

    $u_agent = $_SERVER['HTTP_USER_AGENT'];

    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
    {
        echo date('Y-m-d', strtotime($date));
        //your code
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
       echo date('d-m-Y', strtotime($date));
       //your code
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
       echo date('Y-m-d', strtotime($date));
       //your code
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
       echo date('Y-m-d', strtotime($date));
       //your code
    }
    elseif(preg_match('/Opera/i',$u_agent))
    {
       echo date('Y-m-d', strtotime($date));
       //your code
    }
    elseif(preg_match('/Netscape/i',$u_agent))
    {
       echo date('Y-m-d', strtotime($date));
       //your code
    }



?>
Vikram Jain
  • 5,498
  • 1
  • 19
  • 31