Вопрос

I have data string with data in format like this -> 03/15/2014 (mm/dd/yyyy) and I need to replace that with date in format 2014-03-15 (yyyy-mm-dd).

I know how to change '/' to '-', but I don't know how to turn 2014 from end to start.

Это было полезно?

Решение 3

try this

$dates = explode("/","03/15/2014");
$newdate = $dates[2]."-".$dates[0]."-".$dates[1];

This works in all version of php

Note: the input date should be of format mm/dd/yyyy

If your php is less than 5 you can use this. If php > 5 then it advised to use Datetime class as advised by GordonM in order to find the date is valid or not .

Demo

Другие советы

echo date('Y-m-d', strtotime('03/15/2014'));

Use DateTime::format

 $dt = DateTime::createFromFormat('m/d/Y', ' 03/15/2014');
 echo $dt->format('Y-m-d');
$date1 = '03/15/2014';
$time1 = strtotime($date1);
$date2 = date('Y-m-d', $time);

Just explode the string and replace accordingly:

$str = explode('/', $date);
$result = $str[2].'-'.$str[0].'-'.$str[1];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top