草庐IT

createFromFormat

全部标签

PHP DateTime::createFromFormat 忽略闰年

我必须在日期中转换DayOfYear值。我在2016年的第70天尝试关注:php-r'var_dump(DateTime::createFromFormat("zY","692016"));'classDateTime#1(3){public$date=>string(26)"2016-03-1114:21:07.000000"public$timezone_type=>int(3)public$timezone=>string(3)"UTC"}('z'是基于null的)但这是错误的。应该是2016-03-10!这是一个PHP错误吗? 最佳答案

php - 错误的月份(二月)- DateTime::createFromFormat

这个问题在这里已经有了答案:PHPDateTime::createFromFormatbehavoiur(2个答案)关闭去年。从字符串进行的日期转换返回第二个月(二月)的错误值:$dtformat='Y-m-01';$curDate=DateTime::createFromFormat('Y-m','1996-02');print_r($curDate);$dt=$curDate->format($dtformat);echo$dt."\n";它返回“1996-03-01”而不是“1996-02-01”。这是$currDate数组:DateTimeObject([date]=>1996

php - PHP < 5.3.0 中的 DateTime::createFromFormat

我正在寻找与DateTime::createFromFormat相同的函数但我需要它在运行早于v5.3的PHP版本的环境中工作。基本上我需要提供一种格式,就像您使用Date()一样函数,然后我需要根据该格式解析/验证字符串,如果字符串格式正确且日期有效,则返回时间戳。谁知道我在哪里可以找到类似的东西,还是我必须自己写?同样,这必须使用作为参数提供的特定格式。格式可以是任何格式,所以不能保证我可以使用strtotime(). 最佳答案 DateTime::createFromFormat和date_parse_from_format已

PHP:带时区的 DateTime::createFromFormat()

我想将日期形式从d/m/Y转换为具有时区偏移量的Y-m-d。我可以使用以下代码将d/m/Y转换为Y-m-d:$date=DateTime::createFromFormat('d/m/Y',$date);$date=$date->format('Y-m-d');但我不确定如何添加时区偏移量。 最佳答案 (PHP5>=5.3.0)你实际上输入了第三个参数publicstaticDateTimeDateTime::createFromFormat(string$format,string$time[,DateTimeZone$timez

php - 无法解决此错误:调用未定义的方法 DateTime::createFromFormat()

我正在尝试在php中转换日期格式。以下代码会产生持久性错误$myDate="31/12/1980";$myDateTime=DateTime::createFromFormat('d/m/Y',"$myDate");$newDate=$myDateTime->format('dMY');echo$newDate;包含createFromFormat()的行不断产生错误:““调用未定义的方法”。我的测试Apache服务器和实际服务器都会发生这种情况,它们都运行PHP5.3+我是否需要包含或要求额外的文件?请帮忙-我只是php的中级水平。 最佳答案

PHP DateTime CreateFromFormat 问题

functionvalidateDate($date,$format='m-Y'){$d=DateTime::createFromFormat($format,$date);return$d&&$d->format($format)==$date;}validateDate('09-2017','m-Y');函数是从这个answer复制的或php.net我很困惑为什么它返回false而它在前几个月返回true。有什么想法吗? 最佳答案 这是因为您没有提供日期,所以它默认使用当前日期。当前日期是31日,但9月只有30天,因此会跳到10

php - 使用 DateTime::CreateFromFormat 创建日期对象

我有一串日期:25/08/2012我在这里尝试将其转换为DateTime对象,以便将其保存到MySQL数据库中。我的后端架构有一个准备接收此数据的DateOfPrint日期列。这是我尝试过的:$eventDate=DateTime::createFromFormat('d/m/y',$fecha->innertext);echo$eventDate;echo语句在屏幕上没有显示任何内容,并且在尝试将其保存到数据库时,该列中没有保存任何内容。有什么建议吗? 最佳答案 您的$eventDate包含一个打印为空字符串的bool值(fals

php - DateTime::createFromFormat(...) 有替代品吗?

我有一个代表周数和年份的字符串,我想输出本周的星期日。PHP示例:$dateString='502016';$dateObject=DateTime::createFromFormat('WY',$dateString);echo$dateObject->format('Y-m-d');类似于:{{published_at|format(date("Ymd"),'d-m-Y')}}这在Twig中可能吗? 最佳答案 我找到了可以帮助您的东西:overridingthebuiltintwigdatefilter它正在为你想要的东西创建一

php DateTime createFromFormat 功能

您好,我似乎无法让日期时间方法按预期工作?我可能做错了什么?//PassesOK$dateTime=DateTime::createFromFormat('d/m/Y','12/12/2012');var_dump($dateTime);//shouldfailbutreturns-2016-09-25$dateTime=DateTime::createFromFormat('d/m/Y','56/56/2012');var_dump($dateTime);//correctlyreturnsFalse$dateTime=DateTime::createFromFormat('d/m/

php - 使 DateTime::createFromFormat() 返回子类而不是父类

我正在扩展DateTime,添加一些有用的方法和常量。当使用new创建一个新对象时,一切都很好,但是当使用静态方法createFromFormat时,它总是返回原始的DateTime对象并且当然没有任何子方法可用。我正在使用以下代码来规避此问题。这是最好的方法吗?namespaceNoiseLabs\DateTime;classDateTimeextends\DateTime{staticpublicfunctioncreateFromFormat($format,$time){$ext_dt=newself();$ext_dt->setTimestamp(parent::create