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

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

问题描述

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

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日期 文档.)

(See the strtotime and date documentation on the PHP site.)

请注意,这是对原始问题的快速解决方案.对于更广泛的转换,您确实应该使用 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天全站免登陆