Convert date time with AM PM to 24 hour date format - php

How to convert 10/25/2014 14:00 PM to 2014-10-25 14:00:00 in php?
date('Y-m-d H:i:s', strtotime("10/25/2014 14:00 PM"));
returns
1970-01-01 03:00:00

I don't know what the origin of that date is (how it came to that), but you can use DateTime classes for this:
$raw_date = '10/25/2014 14:00 PM';
// to disregard that PM escape it in the format
$new_date = DateTime::createFromFormat('m/d/Y H:i \P\M', $raw_date);
echo $new_date->format('Y-m-d H:i:s'); // 2014-10-25 14:00:00

$date = "10/25/2014 14:00 PM"; //Here is the date 24 hours format with am/pm
$date = substr($date, 0, -2); //Removed the am/pm from date
echo date('Y-m-d H:i:s', strtotime($date)); //then convert it to mysql date format
Note:
If you use this for date column means replace it data type to int and save unix time stamp which have more comfortable if year limited to 1970 to year 2038.
For reason not to use more than 2038 read here

10/25/2014 14:00 PM your input date format is not correct, you are using 12hr format so time will be 10/25/2014 02:00 PM

Related

display date on page

my date format from my db was Y-m-d h:i:s sample: 2017-08-30 19:19:20 and im trying to display that date on my page..
but the problem is when it displays on my page the date was different compared to the data from my db..
like
data from db was
2017-08-21 18:00:00
2017-08-24 18:00:00
2017-08-30 18:00:00
but the date that was being displayed was
12/31/1969 6:00 PM
12/31/1969 6:00 PM
12/31/1969 6:00 PM
this is the format that i use..
<?php echo date('m/d/Y H:i a',strtotime($date)); ?>
$date is the date from my db..
the date should be
08/21/2017 6:00 PM
08/24/2017 6:00 PM
08/30/2017 6:00 PM
any idea why this happens and any possible solution for this?..
The given lines of php code should be able to give the required output :-
<?php
$dates = [
'2017-08-21 18:00:00',
'2017-08-24 18:00:00',
'2017-08-30 18:00:00',
];
foreach ($dates as $string) {
$date = new DateTime($string);
echo $date->format('m/d/Y g:i A')."\n";
}
Output:
08/21/2017 6:00 PM
08/24/2017 6:00 PM
08/30/2017 6:00 PM
The code snippet is taken from the 3v4l.org.
The other answers seems to answer how to fix it.
I thought I could explain why it happens.
Strtotime fails because it can't parse the date and returns false.
False is the same as 0.
This zero is taken by date and creates a date according to your format.
But because strtotime returned 0 the date returns 0 UNIX time => 1970-01-01 00:00.
And since you are in a timezone (or server at least) with negative difference to UTC a few hours are subtracted from 1970-01-01 00:00 and that leaves you with your output.

convert AMPM to datetime 24 hour

I'm trying to convert 01-31-2017 09:01 AM into 24 hour datetime ( i have AM PM in my values), but it keep giving me 1969-12-31 16:00:00
Here's what I've done:
$old_date = strtotime("01-31-2017 09:01 AM");
$new_date = date('Y-m-d H:i:s', $old_date);
Any kind of help I can get on this is greatly appreciated!
If you know the format of the string you better use the date_create_from_format function:
$s = '01-31-2017 09:01 AM';
$date = date_create_from_format('m-d-Y h:i A', $s);
var_dump($date->format('Y-m-d H:i:s'));
When you use the strtotime you let the php parse the string, and it might lead to a result you are not looking for (for example - 01-02-2017 - is it jan 2nd or feb 1st?).

How to get the right datetime format if this is the current format 'Sat 18 May, 4:18 am'?

I want to convert 'Sat 18 May, 4:18 am' to MySQL datetime format('Y-m-d H:i:s').
$post_datetime = 'Sat 18 May, 4:18 am';
$format = 'Y-m-d H:i:s';
$dt = new DateTime($post_datetime);
echo $dt->format($format);
The problem is the formatted datetime is in future 2016-05-21 04:18:00. I'm assuming this is because there is no year specified. Any solution?
Simply by converting a time string into a timestamp and then generating a date/time string again from that time stamp
date('d-M-Y', strtotime('Thu 1 Jan, 4:09 pm'));

how to php convert this time format?

i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php

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