首页 > 解决方案 > 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));
}

标签: php

解决方案


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)));
}

https://3v4l.org/ZP93WH


推荐阅读