如何将从date_parse返回的日期数组转换回日期字符串 [英] How to convert a date array that was returned from date_parse back into a date string

查看:233
本文介绍了如何将从date_parse返回的日期数组转换回日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期数组,其格式由php例程date_parse返回。我需要将此日期数组转换回日期字符串。

I have a date array in the format which is returned by the php routine date_parse. I need to convert this date array back into a date string.

我正在寻找一个与date_parse例程相反的函数。该函数将接受日期数组作为参数并返回日期字符串。

I am looking for a function which does the reverse of the date_parse routine. That is a function which will accept the date array as a parameter and will return a date string.

http://php.net/manual/zh/function.date-parse.php

我拥有的日期数组有时仅具有年,月和天的值。在其他时间,它将具有年,月,天,小时,分钟和秒的值。如果缺少小时,分钟和秒的值,我希望例程将为字符串的小时,分​​钟和第二部分返回一个带有00:00:00的日期字符串。

The date array I have will sometimes have only values for 'year', 'month', and 'day'. Other times it will have values for 'year', 'month', 'day', 'hour', 'minute', and 'second'. If the hour, minute and second values are missing I would expect that the routine would return a date string with 00:00:00 for the hour, minute, and second part of the string.

我花了一些时间搜索,但到目前为止还没有找到与date_parse相反的函数。

I have spent some time searching, but so far have not found a function that is the reverse of date_parse.

推荐答案

我一直在寻找相同问题的答案,但找不到。我在 PHP文档中使用 date( ) mktime()并提出了这个...

I was looking for an answer to the same question but couldn't find it. I found some examples in the PHP documentation using date() and mktime() and came up with this...

$date_array = date_parse($date_string);

// returns original date string assuming the format was Y-m-d H:i:s
$date_string = date('Y-m-d H:i:s', mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year'])); 

我对此进行了测试,如果时,分和秒都不是,则字符串将包含您想要的零传递给 mktime()

I tested this and the string will contain the zeroes you want if the hour, minute and second are not passed to mktime().

这篇关于如何将从date_parse返回的日期数组转换回日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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