I’m trying to save some records in SQL Server using PHP and Laravel and CSV files, in csv files I have a date with format ‘D. d M. Y, g:i:s A’, like ‘lun. 08 ene. 2024, 1:54:02 p. m.’ and I need to convert it into ‘Y-m-d H:i:s’, but it just return this ‘1970-01-01 00:00:00’ when I use just
i’d tried using date and strtotime like this:
<?php
$newDate = date('Y-m-d H:i:s',strtotime('mon. 08 jan. 2024, 1:54:02 p. m.'));
echo $newDate;
?>
2
Answers
Try first replacing Spanish day and month names with their English counterparts using
str_replace
. Then,DateTime::createFromFormat
should be used to parse this adjusted string into aDateTime
object according to the specified format. And then, the format method of the DateTime object is used to convert the date into the desired'Y-m-d H:i:s'
format. Hope this helpsCustom method for date conversion:
Usage: