我在尝试将字符串转换为时间时遇到了一些麻烦。
我的代码是:
$time = strtotime("14 November, 2013 2:30 AM");
echo $time ."<br />";
echo date("m/d/Y", $time);
我知道strtotime并不神奇,我检查了acceptable date/time formats,但我不知道如何将字符串转换为另一个字符串而不将其转换为时间。
实现这一目标的最简单方法是什么?
答案 0 :(得分:4)
查看DateTime :: createFromFormat,然后在创建的DateTime实例上调用format。
类似的东西:
$yourTimeString = '14 November, 2013 2:30 AM';
$date = DateTime::createFromFormat('d F, Y h:i A', $yourTimeString);
echo $date->format('m/d/Y');
答案 1 :(得分:0)
<?php
// here we assume "day month, year time AMPM"
$date = "14 November, 2013 2:30 AM";
// assign a variable to each part of the string
list($day,$month,$year,$time,$ampm) = explode(" ",$date);
// remove the commas at the end of the month
$month = str_replace(',','',$month);
// Now we rewrite the strtotime string
$time = strtotime($month . " " . $day . ", " . $year . " " . $time . " " . $ampm);
echo $time ."<br />";
echo date("m/d/Y", $time);
答案 2 :(得分:0)
Php date()
函数允许使用自然语言字符串来解析日期,
for Ex:
echo date("d-M-Y", strtotime("first monday of 2019-07")); // returns first monday of july 2019
echo date("d-M-Y", strtotime("last sat of July 2008"));
您可以找到here php指令来将日期解析为自然语言。