在PHP中转换日期格式 [英] Convert a date format in PHP

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

问题描述

我正在尝试将日期从yyyy-mm-dd转换为dd-mm-yyyy(但不在SQL中);但是我不知道date函数如何需要时间戳,并且无法从该字符串获取时间戳.

I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string.

这怎么可能?

推荐答案

使用strtotime()date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

(请参见 strtotime 日期文档.)

请注意,这是对原始问题的快速解决方案.要进行更广泛的转换,您实际上应该使用 DateTime 类进行解析和格式:-)

Note that this was a quick solution to the original question. For more extensive conversions, you should really be using the DateTime class to parse and format :-)

这篇关于在PHP中转换日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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