如何在 PHP 中重新格式化日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2754765/
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 07:34:33  来源:igfitidea点击:

How to reformat date in PHP?

phpdate

提问by Lisa

I am pulling the dates of various posts from a database. The dates are in the following format:

我正在从数据库中提取各种帖子的日期。日期格式如下:

2009-08-12

Numeric Year - Numeric Month - Numeric Day

How can I reformat these dates to something more user friendly like:

如何将这些日期重新格式化为对用户更友好的格式,例如:

August 12, 2009

Numeric Month Numeric Date, Numeric Year

Assuming that the date gotten from the mysql database is stored in a variable called:

假设从 mysql 数据库获取的日期存储在一个名为的变量中:

$date = $row['date_selected'];

回答by Billy ONeal

Unlike the strtotimebased examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.

strtotime基础示例不同,这允许您确保以正确的顺序解释月份和日期,而不管服务器上指定的区域设置如何。

$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');

回答by Femaref

date("F d, Y", strtotime($input))

date("F d, Y", strtotime($input))

回答by Tyler Carter

$new_format = date("Your Date String", strtotime($date));

See:
- http://php.net/strtotime
- http://php.net/date

请参阅:
- http://php.net/strtotime
- http://php.net/date

Basically, if strtotime()can read it correctly, you can reformat it anyway you please.

基本上,如果strtotime()可以正确阅读它,您可以随意重新格式化它。

In this case, Year - Month - Dayis a properly recognized strtotime()format, this might not be the case for other formats.

在这种情况下,Year - Month - Day是一种正确识别的strtotime()格式,其他格式可能不是这种情况。

回答by gurun8

You might consider doing your date formatting in MySQL with your select statement:

您可能会考虑使用 select 语句在 MySQL 中进行日期格式化:

DATE_FORMAT(date,'%M %e, %Y') as date_selected

http://www.w3schools.com/sql/func_date_format.asp

http://www.w3schools.com/sql/func_date_format.asp

回答by user3586571

<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result

NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
    $dateFinal = '';
    if($format == 'm'.$delimS.'d'.$delimS.'Y'){
        $dateFinal_exp = explode($delimS,$date);
        $dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
    }else if($format == 'Y'.$delimS.'m'.$delimS.'d'){

        $dateFinal_exp = explode($delimS,$date);
        $dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
    }
    return $dateFinal;
}
?>

回答by icio

<?php
echo date('F j, Y', strtotime($date));

回答by Weston C

You might want to look at the php function strtotime:

您可能想查看 php 函数strtotime

http://php.net/manual/en/function.strtotime.php

http://php.net/manual/en/function.strtotime.php

It'll parse a large number of date representations to a Unix timestamp.

它会将大量日期表示解析为 Unix 时间戳。

Then use the datefunction.

然后使用该date功能。

回答by Oyeme

Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

像这样使用它:
// 2005 年 2 月 1 日
打印日期 ("F j, Y", mktime (0,0,0,14,1,2004));

回答by jkilbride

Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php

使用 strtodate 或 purge 将日期拆分为不同的组件,然后您可以使用具有适当格式字符串的日期函数:http: //php.net/manual/en/function.date.php

$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));

Outputs: "August 12, 2009"

输出:“2009 年 8 月 12 日”