在以dd / mm / yyyy格式指定日期时,strtotime()没有返回正确的值 [英] strtotime() does not return correct value when specifying date in dd/mm/yyyy format

查看:86
本文介绍了在以dd / mm / yyyy格式指定日期时,strtotime()没有返回正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将日期 24/09/2010 转换为格式 dd / mm / yyyy 2010-09-24 格式为 yyyy-mm-dd

I want to convert date 24/09/2010 in format dd/mm/yyyy to 2010-09-24 in format yyyy-mm-dd.

此方法有效:

date("Y-m-d",strtotime("09/24/2010"));

但这不是:

date("Y-m-d",strtotime("24/09/2010")); // it returns '1970-01-01'

知道为什么吗?

推荐答案

strtotime 尽最大努力猜测给定字符串时的含义,但是可以无法处理所有日期格式。在您的示例中,可能是您想引用无效的第24个月,并返回0,然后将该日期视为unix纪元(获取日期)。

strtotime does its best to guess what you mean when given a string, but it can't handle all date formats. In you example, it is probably thinking that you are trying to refer to the 24th month, which isn't valid, and returns 0, which date then treats as the unix epoch (the date you got).

您可以使用 mktime()解决此问题和 explode()函数,例如:

you can get around this using the mktime() and explode() functions, like so:

$date = "24/09/2010";
$dateArr = explode("/",$date);
$timeStamp = mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]);
$newFormat = date("Y-m-d",$timeStamp);

这篇关于在以dd / mm / yyyy格式指定日期时,strtotime()没有返回正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆