简体   繁体   English

将数据格式从mm / dd / yyyy更改为yyyy-mm-dd

[英]Change data format from mm/dd/yyyy to yyyy-mm-dd

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) . 我有数据字符串,其数据格式如下-> 03/15/2014 (mm/dd/yyyy) ,我需要将其替换为日期,格式为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. 我知道如何将'/'更改为'-',但是我不知道如何从头到尾翻过2014年。

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

Use DateTime::format 使用DateTime::format

 $dt = DateTime::createFromFormat('m/d/Y', ' 03/15/2014');
 echo $dt->format('Y-m-d');

try this 尝试这个

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

This works in all version of php 这适用于所有版本的php

Note: the input date should be of format mm/dd/yyyy 注意:输入日期的格式应为mm/dd/yyyy

If your php is less than 5 you can use this. 如果您的php小于5,则可以使用它。 If php > 5 then it advised to use Datetime class as advised by GordonM in order to find the date is valid or not . 如果php > 5则建议按照GordonM建议使用Datetime类,以查找日期是否有效。

Demo 演示版

$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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM