在 PHP 中格式化日期字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3654101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:34:13  来源:igfitidea点击:

Format a date string in PHP

phpdate

提问by cannyboy

If I have a string which represents a date, like "2011/07/01" (which is 1st July 2011) , how would I output that in more readable forms, like:

如果我有一个表示日期的字符串,例如 "2011/07/01" (即 2011 年 7 月 1 日),我将如何以更易读的形式输出它,例如:

1 July 2011
1 Jul 2011  (month as three letters)

And also, how could I make it intelligently show date ranges like "2011/07/01" to "2011/07/11" as

而且,我怎样才能让它智能地将日期范围显示为“2011/07/01”到“2011/07/11”

1 - 11 July 2001

(without repeating the 'July' and '2011' in this case)

(在这种情况下不重复“七月”和“2011年”)

回答by NullUserException

You can convert your date to a timestamp using strtotime()and then use date()on that timestamp. On your example:

您可以使用将日期转换为时间戳strtotime(),然后date()在该时间戳上使用。在你的例子中:

$date = date("j F Y", strtotime("2011/07/01")); // 1 July 2011
$date = date("j M Y", strtotime("2011/07/01")); // 1 Jul 2011

回答by Richard Fearn

As NullUserException mentioned, you can use strtotimeto convert the date strings to timestamps. You can output 'intelligent' ranges by using a different date format for the first date, determined by comparing the years, months and days:

正如 NullUserException 提到的,您可以使用strtotime将日期字符串转换为时间戳。您可以通过对第一个日期使用不同的日期格式来输出“智能”范围,通过比较年、月和日来确定:

$date1 = "2011/07/01";
$date2 = "2011/07/11";

$t1 = strtotime($date1);
$t2 = strtotime($date2);

// get date and time information from timestamps
$d1 = getdate($t1);
$d2 = getdate($t2);

// three possible formats for the first date
$long = "j F Y";
$medium = "j F";
$short = "j";

// decide which format to use
if ($d1["year"] != $d2["year"]) {
    $first_format = $long;
} elseif ($d1["mon"] != $d2["mon"]) {
    $first_format = $medium;
} else {
    $first_format = $short;
}

printf("%s - %s\n", date($first_format, $t1), date($long, $t2));

回答by Ciges

I would use strtotimeANDstrftime. Is a much simpler way of doing it.

我会使用strtotimeANDstrftime。是一种更简单的方法。

By example, if a have a date string like "Oct 20 18:29:50 2001 GMT" and I want to get it in format day/month/year I could do:

例如,如果有一个像“Oct 20 18:29:50 2001 GMT”这样的日期字符串,我想以日/月/年的格式获取它,我可以这样做:

$mystring = "Oct 20 18:29:50 2001 GMT";
printf("Original string: %s\n", $mystring);
$newstring = strftime("%d/%m/%Y", strtotime($mystring));
printf("Data in format day/month/year is: %s\n", $newstring);

回答by Robus

As for the second one:

至于第二个:

$time1 = time();
$time2 = $time1 + 345600; // 4 days
if( date("j",$time1) != date("j",$time2) && date("FY",$time1) == date("FY",$time2) ){
   echo date("j",$time1)." - ".date("j F Y",$time2);
}

Can be seen in action here

可以在这里看到在行动

Just make up more conditions

多补些条件