简体   繁体   English

PHP date()偶尔工作

[英]PHP date() working sporadically

I am converting two strings from one date format to another but I am getting some unusual responses 我正在将两个字符串从一种日期格式转换为另一种日期格式,但我得到了一些不同寻常

The two dates i am converting are: 我转换的两个日期是:

1: 2002-09-23 1:2002-09-23

2: 2010-03-25 2:2010-03-25

The PHP code is as follows for each of the dates: 每个日期的PHP代码如下:

1: date('d F Y', strtotime((string)$report_display->arr_output_base['Date1']['value'])); 1: date('d F Y', strtotime((string)$report_display->arr_output_base['Date1']['value']));

2: date("d FY", strtotime((string)$report_display->arr_input_base['Date2']['value'])); 2: date("d FY", strtotime((string)$report_display->arr_input_base['Date2']['value']));

The responses I'm getting are as follows: 我得到的答复如下:

1:Sometimes 31 December 1986 which is wrong but then other times I'll get 23 September 2002 which is right 1:有时候1986年12月31日这是错误的,但有时我会在2002年9月23日这是对的

2: is always 25 March 2010 2:始终是2010年3月25日

When you know the format, why take risks sending it to strtotime and not use a proper method that uses the exact format? 当您知道格式时,为什么要冒险将其发送到strtotime并且不使用使用确切格式的正确方法?

$date = DateTime::createFromFormat('Y-m-d', '2002-09-23');
echo $date->format('d F Y');

That way there is no guesswork involved as to whether a month is a month or it is a day. 这样就没有猜测一个月是一个月还是一天。 That will always return the same date no matter what. 无论如何,这总是会返回相同的日期。

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

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