简体   繁体   English

PHP : 将 YYYY-MMM-DD 转换为 YYYY-MM-DD

[英]PHP : Convert YYYY-MMM-DD to YYYY-MM-DD

I need to convert date string into number:我需要将日期字符串转换为数字:

2012-Sep-01 to 2012-09-01

Any idea?任何的想法? Regards,问候,

This might work for you:这可能对你有用:

echo date("Y-m-d", strtotime($yourCurrentDateVat))'

PHP website is down at the moment, but here is some extra info on the date function. PHP 网站目前已关闭,但这里有一些关于日期函数的额外信息

Try to use DateTime PHP class:尝试使用 DateTime PHP 类:

$date = "2012-Sep-01";
$result = DateTime::createFromFormat("Y-M-d", $date)->format("Y-m-d");

使用strtotime()

echo date('Y-m-d', strtotime('2012-Sep-01'));
$date = DateTime::createFromFormat('Y-M-j', '2012-Sep-01');
echo $date->format('Y-m-d');

Actually stolen from the manual: http://us.php.net/manual/en/datetime.createfromformat.php实际上是从手册中窃取的: http : //us.php.net/manual/en/datetime.createfromformat.php

You should use strtotime() .您应该使用strtotime() More info on here .更多信息在这里 And then date() to create a new string.然后date()创建一个新字符串。 More info on that here .更多信息在这里

$date = '2012-Sep-01'; //Sets your date string
$time_epoch = strtotime($date); //Converts the string into an epoch time
$new_date = date('Y-m-d', $time_epoch); //Creates a new string
$timestamp = strtotime('22-09-2008');
$new_date = date("y-m-d", $timestamp);

嗨,尝试在 PHP 中使用它,你肯定会得到你想要的,我正在寻找相同的但没有找到但后来发现我在某处使用了日期时间公式并对其进行了修改以获得这个。

$getdatetime=date_create('2020-May-21'); $dateconverted=date_format($getdatetime,'Ym-d'); echo $dateconverted;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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