gpt4 book ai didi

php日期格式转换问题

转载 作者:行者123 更新时间:2023-12-04 23:33:58 24 4
gpt4 key购买 nike

这里是我尝试将日期转换为不同格式的函数。

/*  example:
* dateString = '03/25/2010';
* inputDateFormat = '%m/%d/%Y';
* ouputDateFormat = 'Y-m-d';
* return -> '2010-03-25';
*/
function formatDate($dateString,$inputFormat=NULL,$outputFormat=NULL){
if($dateString==''||$dateString==NULL) return '';
$t = strptime($dateString,$inputFormat);
return gmdate($outputFormat,mktime($t[tm_sec],$t[tm_min],$t[tm_hour],($t[tm_mon]+1),($t[tm_mday]+1),($t[tm_year]+1900)));
}

我的问题是

当我尝试将此日期 Wed, 19 Jan 2011 21:16:37 +0000 转换为 2011-01-19 21:16:37 时,使用以下代码行:

echo formatDate('Wed, 19 Jan 2011 21:16:37 +0000','%a, %d %b %Y %H:%M:%S','Y-m-d H:i:s');

结果是这样的:

2011-01-21 11:16:21

为什么我的约会时间多了 2 天。你有什么想法吗?

最佳答案

改用这个:

  function formatDate($dateString, $outputFormat=NULL){
return date($outputFormat, strtotime($dateString));
}

echo formatDate('Wed, 19 Jan 2011 21:16:37 +0000','Y-m-d H:i:s');

关于php日期格式转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7218017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com