从时间戳中获取日期将返回月份


Getting the day from a timestamp returns the month

我有以下日期时间:

09/05/2016 09:12 (9 May 2016)

我想从中获得乐趣,所以我做

date('d', strtotime($date))

但是返回05,也就是月份。

我做错了什么?

这是因为{J}认为这是一种美国日期格式。

尝试将斜线(/)更改为斜线(-)

// that is recognized as day month year, not month/day/year
09-05-2016

如果你不能获得不同格式的日期,你可以做一个str_replace来删除斜杠

$date = str_replace('/', '-', $date);

这里有一个链接可能会对你有所帮助。

使用DateTime类将为您提供更多的灵活性。例如:看看createFromFormat方法,它可以将任何格式的日期和时间转换为有效的DateTime对象。

// Define your date
$date = '09/05/2016 09:12';
// convert it to a DateTime object
$date = DateTime::createFromFormat('d/m/Y H:i', $date);
// Output - returns 09
echo $date->format('d');

有关更多信息,请参阅PHP的DateTime文档。

通过date()首先设置日期时间的格式。那就结束你的一天吧!

$date = date("d/m/Y h:i",strtotime($date));//format your date as `09/05/2016 09:12`
var_dump(date("d",strtotime($date)));

如果您知道日期的格式不正确,请根据需要重新格式化。只要按照自己的意愿创建约会即可。

date_create将从您的字符串中创建一个新日期。date_format

将您的日期转换为新格式最后是带有的date功能

strtotime将显示您想要的结果。

$str = "09/05/2016 09:12";
$date = date_create($str);
$time = date_format($date, "m-d-Y h:i");
echo date("d", strtotime($time)); //09