使用 PHP 从 MySQL 日期时间转换为另一种格式

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

Convert from MySQL datetime to another format with PHP

phpmysqldatetime

提问by Tim Boland

I have a datetimecolumn in MySQL.

datetime在 MySQL 中有一个列。

How can I convert it to the display as mm/dd/yy H:M (AM/PM)using PHP?

如何使用 PHP将其转换为mm/dd/yy H:M (AM/PM) 显示

回答by kta

If you're looking for a way to normalize a date into MySQL format, use the following

如果您正在寻找一种将日期规范化为 MySQL 格式的方法,请使用以下命令

$phpdate = strtotime( $mysqldate );
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );

The line $phpdate = strtotime( $mysqldate )accepts a string and performs a series of heuristics to turn that string into a unix timestamp.

该行$phpdate = strtotime( $mysqldate )接受一个字符串并执行一系列启发式方法以将该字符串转换为 unix 时间戳。

The line $mysqldate = date( 'Y-m-d H:i:s', $phpdate )uses that timestamp and PHP's datefunction to turn that timestamp back into MySQL's standard date format.

该行$mysqldate = date( 'Y-m-d H:i:s', $phpdate )使用该时间戳和 PHP 的date函数将该时间戳转换回 MySQL 的标准日期格式。

(Editor Note: This answer is here because of an original question with confusing wording, and the general Google usefulness this answer provided even if it didnt' directly answer the question that now exists)

编者注:这个答案在这里是因为一个措辞混乱的原始问题,以及这个答案提供的一般谷歌有用性,即使它没有直接回答现在存在的问题)

回答by Tim Boland

To convert a date retrieved from MySQL into the format requested (mm/dd/yy H:M (AM/PM)):

将从 MySQL 检索到的日期转换为请求的格式 ( mm/dd/yy H:M (AM/PM)):

// $datetime is something like: 2014-01-31 13:05:59
$time = strtotime($datetimeFromMysql);
$myFormatForView = date("m/d/y g:i A", $time);
// $myFormatForView is something like: 01/31/14 1:05 PM

Refer to the PHP date formatting optionsto adjust the format.

请参阅PHP 日期格式选项来调整格式。

回答by enobrev

If you are using PHP 5, you can also try

如果你使用的是 PHP 5,你也可以试试

$oDate = new DateTime($row->createdate);
$sDate = $oDate->format("Y-m-d H:i:s");

回答by Tony Stark

$valid_date = date( 'm/d/y g:i A', strtotime($date));

Reference:http://php.net/manual/en/function.date.php

参考:http : //php.net/manual/en/function.date.php

回答by Hasenpriester

Finally the right solution for PHP 5.3 and above: (added optional Timezone to the Example like mentioned in the comments)

最后是 PHP 5.3 及更高版本的正确解决方案:( 在示例中添加了可选的时区,如评论中所述)

$date = \DateTime::createFromFormat('Y-m-d H:i:s', $mysql_source_date, new \DateTimeZone('UTC'));
$date->setTimezone(new \DateTimeZone('Europe/Berlin')); // optional
echo $date->format('m/d/y h:i a');

回答by flash

An easier way would be to format the date directly in the MySQL query, instead of PHP. See the MySQL manual entry for DATE_FORMAT.

一种更简单的方法是直接在 MySQL 查询中格式化日期,而不是 PHP。请参阅DATE_FORMATMySQL 手册条目

If you'd rather do it in PHP, then you need the datefunction, but you'll have to convert your database value into a timestamp first.

如果您更愿意在 PHP 中执行此操作,那么您需要date函数,但您必须先将数据库值转换为时间戳。

回答by nixis

Forget all. Just use:

忘记所有。只需使用:

$date = date("Y-m-d H:i:s",strtotime(str_replace('/','-',$date)))

回答by Greg

To correctly format a DateTimeobject in PHP for storing in MySQL use the standardised format that MySQL uses, which is ISO 8601.

DateTime在 PHP 中正确格式化对象以存储在 MySQL 中,请使用 MySQL 使用的标准化格式,即ISO 8601

PHP has had this format stored as a constant since version 5.1.1, and I highly recommend using it rather than manually typing the string each time.

PHP 自 5.1.1 版起就将此格式存储为常量,我强烈建议使用它,而不是每次都手动输入字符串。

$dtNow = new DateTime();
$mysqlDateTime = $dtNow->format(DateTime::ISO8601);

This, and a list of other PHP DateTime constants are available at http://php.net/manual/en/class.datetime.php#datetime.constants.types

这和其他 PHP DateTime 常量的列表可在http://php.net/manual/en/class.datetime.php#datetime.constants.types 获得

回答by riz

This should format a field in an SQL query:

这应该格式化 SQL 查询中的字段:

SELECT DATE_FORMAT( `fieldname` , '%d-%m-%Y' ) FROM tablename

回答by Gustavo Carreno

Use the datefunction:

使用日期函数:

<?php
    echo date("m/d/y g:i (A)", $DB_Date_Field);
?>