wrong convert date() PHP - 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

Related

Convert Time stamp to readable format

I need to conver timestamp to 2016-07-12 format. This is what I tried.
$selectedDate=date('m/d/Y H:i:s', '1465430400000');
I got 08/23/48407 00:00:00 I need to conver it to 2016-07-12 format.
Please Note: Here the format m/d/Y H:i:s isn't the matter. I'm getting wrong date is the problem
Any suggetion would be appricieated.
It looks like your timestamp is 1000x what date() expects, so try first dividing it by 1000 (and then, of course, use the right date format):
$selectedDate = date('Y-m-d', 1465430400000/1000);
You can convert Date in Any format:
<?php $date1 = strtotime($old_date);
echo $date = date("y-M-d", $date1); ?>
Complete list of format options

Convert datepicker time to mysql datetime format in 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

PHP - strftime (minutes and seconds ignored)?

I tried to save time in format YYYY-mm-dd 23:59:59 to mysql database column with datetime. I don't understand why minutes and seconds are ignored always 00 ? Thank you very much for help.
PHP:
$time = strftime('%Y-%m-%d %H:%i:%s', time());
Output:
2014-07-16 11:00:00
You can simple use:
$time = date("Y-m-d H:i:s");
or even better use mysql NOW() function
you have to specify timezone as follows.
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions.
you can use mysql function
now()
You are looking for
$time = strftime('%Y-%m-%d %H:%M:%S', time());
from the documentation
%S Two digit representation of the second
%M Two digit representation of the minute 00 through 59
Also, what PHP version are you using? %i:%s should not be 00:00

how to convert 12h to 24h in php

I want to convert 12h time in php which is in this format: 05/31/2012 09:48 AM
to 24h time format : 2011-11-27 11:53:36
This is my code line that you can change:
$time= $_POST['time'];
You can use this:
// $time = 05/31/2012 09:48 AM
$time = $_POST['time'];
//output new time: 2012-05-31 09:48:00
$newTime = date("Y-m-d H:i:s", strtotime($time)) );
live example
Doc:
strtotime
date
http://psoug.org/snippet/Convert_12_to_24_hour_time_and_vice_versa_241.htm
You have to use strtotime like this:
$date="05/31/2012 09:48 AM";
echo date("Y-m-d H:i:s",strtotime($date));
go through below link for date functionality in PHP
http://php.net/manual/en/function.date.php
You can use strtotime and then date functions for that.
http://php.net/manual/en/function.date.php All the possible options are listed there.
The first argument to date is the format and the second is a timestamp (which you get with strtotime of the current date string you have).
date('Y-m-d H:i:s', strtotime($your_current_date_string));
The format is just off the top of my head and not exact. You can look up the format you need in the table in the link provided.

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