Convert datepicker time to mysql datetime format in PHP - php

Is there a way I can convert a date in this format: 08/19/2014 1:45 pm
into MySQL datetime format like 2014-08-19 13:45:00.
I tried using something like
date("Y-m-d H:i:s", $myTime);
but I don't think it likes the 'pm' and gives back 1969-12-31, giving error:
"A non well formed numeric value encountered"

Have you tried using strtotime()?
$myTime = strtotime("08/19/2014 1:45 pm");
echo date("Y-m-d H:i:s", $myTime);
Output:
2014-08-19 13:45:00

This works on dd/mm/yyyy
$date = '18/03/2016 16:25';
echo date("Y-m-d H:i:s",strtotime(str_replace('/','-',$date)));
=> 2016-03-18 16:25:00

Related

wrong convert date() PHP

I used date() to convert 12H time to 24H using this code
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46 PM"));
echo $over;
but the output is this below:
1969-12-31 16:00:00
How to get rid of this, is this a bug? or my code?
sandbox
13:42:46 PM isn't 12h time format (PM is nonsense in 24h format), 01:42:46 PM is correct.
Just specify the correct date format (PM is not supported):
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46"));
echo $over;
Which date format is supported you can find in https://www.php.net/manual/de/datetime.formats.php

Datetime with AM/PM rconversion to standard format into database in laravel 5.8

I've a date in the following format
$date = "6.17.2019 12:00:00 AM."
I need to convert this into Y-m-d h:i:s.
I tried the following line of code,
$regdate = \Carbon\Carbon::parse($date)->format('Y-m-d h:i:s');
But it results in error.
DateTime::__construct(): Failed to parse time string (6.17.2019 12:00:00 AM.) at position 7 (1): Unexpected character"
Desired output is :
2019-06-17 00:00:00
yo can change format to carbon then can be change to any format.
$date = "6.17.2019 12:00:00 AM.";
$regdate = \Carbon\Carbon::createFromFormat('m.d.Y h:i:s A.', '6.17.2019 12:00:00 AM.');
$regdate = \Carbon\Carbon::parse($regdate)->format('Y-m-d');
echo $regdate;
I assume whatever this Carbon thing is extends the PHP DateTime class?
Use createFromFormat()
$date = DateTime::createFromFormat('d.m.Y H:i:s A.', '6.17.2019 12:00:00 AM.');
echo $date->format('Y-m-d H:i:s');
which will give you:
2020-05-06 00:00:00
Check it out here https://3v4l.org/g53Xp
And read up on PHP's DateTime class https://www.php.net/manual/en/datetime.createfromformat.php

MM-YYYY to YYYY-MM-DD hh:mm:ss on php and mysql

If i have a string with format "MM-YYYY" like 04-2016,
how to get them into datetime format mysql which is "YYYY-MM-dd hh:mm:ss".
I wonder like this : 2016-04-01 00:00:00,
I am using php with mysql.
$d = '04-2016';
$date = DateTime::createFromFormat('d-m-Y', ('01-'.$d));
$date->setTime(0, 0, 0);
echo $date->format('Y-m-d H:i:s'); // Outputs 2016-04-01 00:00:00
Something like the above will allow you to get the format you want in PHP, then you just have to insert it into your database as you currently are.
you have date time formate with Am and pm then use below code
$d='03-03-2016 03:07:22';
echo date('Y-m-d h:i:s a',strtotime($d));

PHP Add 4 hours to date [duplicate]

This question already has answers here:
Add hours to datetime in PHP
(1 answer)
How to add an hour onto the time of this datetime string?
(3 answers)
php string in a date format, add 12 hours
(4 answers)
Closed 1 year ago.
I'm trying to add four hours to a date but the resultant time is incorrect
$date = $dates[0] < $dates[1] ? $dates[1] : $dates[0];
$new_date = date("Y-m-d H:i:s", strtotime('+4 hours', $date));
echo $date;
echo "<br/>". $new_date;
The result i'm getting is
2015-09-17 09:36:18
1969-12-31 20:33:35
The first date is correct but the second isn't, it should be 4 hours ahead of the first
Does this work?
$date = $dates[0] < $dates[1] ? $dates[1] : $dates[0];
$new_date = date("Y-m-d H:i:s", strtotime('+4 hours', strtotime($date)));
echo $date;
echo "<br/>". $new_date;
You were passing $date AS the second argument of strtotime, and it's not a UNIX timestamp (as you said). So with strtotime again, you should convert it in a UNIX timestamp, and then pass it to strtotime.
Add timestamps to a timestamp, not to a date string.
echo date("Y-m-d H:i:s", strtotime('+4 hours', strtotime("2015-09-17 09:36:18")));
^
Fiddle
That is the right syntax. You can simplify it however with
echo date("Y-m-d H:i:s", strtotime('2015-09-17 09:36:18 +4 hours'));
Fiddle
You're using strtotime incorrectly. Its second argument MUSt be a unix timestamp:
php > var_dump(strtotime('+4 hours', 12345));
int(26745)
php > var_dump(strtotime('+4 hours', '2015-09-17 09:36:18'));
PHP Notice: A non well formed numeric value encountered in php shell code on line 1
int(16415)
Your call should be
php > var_dump(date('r', strtotime('2015-09-17 09:36:18 + 4 hours')));
^^^^^^^^^^^
string(31) "Thu, 17 Sep 2015 13:36:18 -0500"
php >
Both other answers are correct.
Not seeing what you have in your $dates array makes it a bit tricky to give exact answer :) but in the following example i assumed you have a well formatted string date, in which case you need to convert it back to the timestamp using same method (strtotime) before adding what you like into it .
$date = (new DateTime('now'))->format('Y-m-d H:i:s');
$new_date = date('Y-m-d H:i:s', strtotime('+4 hours', strtotime($date)));
echo $date . PHP_EOL . $new_date;
In other word, if your $date array contains UNIX time stamp then your original code should work
$date = (new DateTime('now'))->getTimestamp();
$new_date = date("Y-m-d H:i:s", strtotime('+4 hours', $date));

How to convert time like 2 pm or 3 am to HH:MM:SS format in php?

How can I convert times like 2 pm or 3 am to HH:MM:SS format in php?
Like this:
$date = '2pm';
echo date('H:i:s', strtotime($date));
Result:
14:00:00
And:
$date = '2am';
echo date('H:i:s', strtotime($date));
Result:
02:00:00
More Info:
date
strtotime
Update:
To convert it back:
$date = '14:00:00';
echo date('HA', strtotime($date));
Result:
14PM
(this should be a comment, but it's too long)
The reason Sarfraz's solution is incomplete is because it doesn't account for DST transaitions. During DST transitions, some hours may not exist.
Consider the timezone is Europe/Lisbon and we're in March 28th 2010, when DST kicked in.
When we hit 1am, we change from UTC+0 to UTC+1, i.e., we skip 1 hour. Example:
date_default_timezone_set("Europe/Lisbon");
$date = '2010-03-28 1am';
$date2 = '2010-03-28 1:30am';
echo date('H:i:s', strtotime($date)),"\n";
echo date('H:i:s', strtotime($date2)),"\n";
gives
02:00:00
02:30:00
Therefore, Sarfraz solution will fail unless when you say you want to convert 1am to 01:00, these times always refer to the current day in the server's timezone.

Categories