使用 PHP 从 MySQL 日期时间转换为另一种格式 [英] Convert from MySQL datetime to another format with PHP

查看:27
本文介绍了使用 PHP 从 MySQL 日期时间转换为另一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MySQL 中有一个 datetime 列.

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

解决方案

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

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

$phpdate = strtotime( $mysqldate ) 接受一个字符串并执行一系列试探法将该字符串转换为 unix 时间戳.

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

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

I have a datetime column in MySQL.

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

解决方案

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

$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.

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

(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)

这篇关于使用 PHP 从 MySQL 日期时间转换为另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆