PHP转换日期格式 [英] PHP converting date format
问题描述
Duplicate
- Managing date formats differences between PHP and MySQL
- PHP/MySQL: Convert from YYYY-MM-DD to DD Month, YYYY?
- Format DATETIME column using PHP after printing
- date formatting in php
亲爱的,
我有一个PHP页面,我将从Mysql数据库显示一些数据。
我有2个日期显示在此页面上。在我的数据库表中,日期1的格式为d / m / Y(例如:11/11/2002),日期2的格式为dmY(例如: 11-11-2002)
我需要以相同的格式显示这两个格式。我已经存储在变量$ dateFormat ='m / d / Y'
I have a PHP page where i wil be displaying some data from Mysql db. I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002) I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'
任何人可以指导我
提前感谢
推荐答案
使用 strtotime
将字符串转换为Unix时间戳,然后使用 date
函数生成正确的输出格式。
Use strtotime
to convert the strings into a Unix timestamp, then use the date
function to generate the correct output format.
由于您使用的是英国日期格式d / m / Y,而strtotime需要使用美国格式,因此您需要以不同的方式转换:
Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:
$date1 = "28/04/2009";
$date2 = "28-04-2009";
function ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
$date1 = date($dateFormat, ukStrToTime($date1));
$date2 = date($dateFormat, ukStrToTime($date2));
这篇关于PHP转换日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!