php - strtotime making all dates have the current year
问题描述
Working on a legacy application that is getting replaced at the end of next year. I am trying to modify how it writes some of the dates into the database and I'm just not familiar enough with PHP to figure out why it is doing this:
There is a date selector on the front-end. If you pick something like 1 Dec, 2019, it goes through jquery retaining that date, right before it is passed into the below function it is right, then it gets into the function and the strtotime($str)
messes it up.
It converts 1 Dec, 2019 into 1543724340
which dropping into an UNIX timestamp shows it as 1 Dec, 2018.
How do I prevent this? I'm just trying to get the date for what the user sets in the calendar.
function str2date($str,$format="Y-m-d") {
return date($format, strtotime($str));
}
解决方案
Just remove the comma from the string with str_replace and strtotime can parse it correctly.
function str2date($str,$format="Y-m-d") {
return date($format, strtotime(str_replace(",", "", $str)));
}
推荐阅读
- ftp - 使用 WinSCP 在日期范围内下载文件时“缺少命令 'get' 的参数”
- python - 高维中的numpy非规范点积
- sql-server - 使用 Powershell 脚本输出文件具有相同的重复行
- r - Simba Athena ODBC:无法使用 SQLGetPrivateProfileString 函数
- flutter - 颤动的riverpod ConsumerWidget没有被重建
- typescript - 打字稿:根据参数在函数响应中包含键
- typescript - 不在“rootDir”“c:/Users/hasit/Desktop/typescript/src”下。'rootDir' 应包含所有源文件。”
- installation - 当包管理器尝试从命令行安装它们时,是否可以对所需的 nuget 列表进行排序以使其不崩溃?
- reactjs - React.js:如何在 React 中显示生日字段直到日期?
- android - 当我在 MAINACTIVTY 中添加 INTENT 后尝试运行我的应用程序时,它显示此错误