convert AMPM to datetime 24 hour - php

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?).

Related

Convert date time with AM PM to 24 hour date format

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

Get timestamp of beginning of day given 'Y-m-d'

Given a date string formatted Y-m-d (2014-4-11 for example), how can I get a UNIX timestamp of 12am the beginning of that day? Would it involve date_parse_from_format()?
Thanks
You can simply use strtotime()
$date = "2014-04-11";
$timestamp = strtotime($date);
which inturm gives you -
$d = date('Y-m-d H:i:s', $timestamp); // 2014-04-11 00:00:00
Try online conversion to test - http://www.onlineconversion.com/unix_time.htm

php convert 12hr to 24hr 12:00:am -> 00:00:00

I am getting a datetime format from a form in this format
// $start equals 2014-04-19 12:00:am
// I need to convert to 24hr 2014-04-19 00:00:00
$startconvert = str_replace(':am', '', $start);
$startconverted = date( 'Y-d-m H:i:s', strtotime($startconvert));
echo($startconverted);
//becomes 2014-19-04 12:00:00 which is 12pm
Anyone know how to do this? This looks right to me but it ends up being 12pm
$date = DateTime::createFromFormat('Y-m-d h:i:a', '2014-04-19 12:00:am');
echo $date->format('Y-m-d H:i:s');
This just takes the date you have and reads it in by its parts using DateTime::createFromFormat(). That DateTime object can then be formatted normally.

How convert date format in php

hello guys i have date like this
25 March 2014 - 16:45
i want to convert this date in to this format how to do that please Help
2014-03-25 16:45:00
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45'));
but it's not work
i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45')); but it's not work
The reason your code doesn't work is because '25 March 2014 - 16:45' is not in a format that strtotime() can parse.
strtottime() is good at handling a wide range of formats, but it can't cope with absolutely anything; there's just too much variation possible.
I suggest that you try PHP's DateTime class instead. This class has a method called createFromFormat() which allows you to specify the format of the incoming date string. This makes it easier for it to parse, and allows for formats that might not be recognised otherwise, like yours.
Try this:
$date = DateTime::createFromFormat('j F Y - H:i', '25 March 2014 - 16:45');
echo $date->format('Y-m-d H:i:s');
Use PHP DateTime
You have "-" in "25 March 2014 - 16:45" in the string so it not be able to read by DateTime
So work around would be
$str = "25 March 2014 -16:45";
$date = new DateTime(str_replace("-","",$str));
echo $date->format('Y-m-d H:i:s');
I've successfully tried the following with your string:
$a = strptime('25 March 2014 - 16:45', '%d %B %Y - %R');
$time = mktime(
$a["tm_hour"],
$a["tm_min"],
$a["tm_sec"],
$a["tm_mon"]+1,
$a["tm_mday"],
$a["tm_year"]+1900
);
$converted = date('Y-m-d H:i:s',$time);
Try this:
$creation = date('Y-m-d H:i:s',strtotime('25 March 2014 16:45'));

convert from a string into a date in php

I have looked around but keep getting an error.
I have the following string:
$date = "25 Jan 2013 07:30 PM";
but I need to add it into sql as separate date and time fields. Any suggestions how I can convert it to a date rather than a string? When I try to using something like
strtotime($date);
I end up with:
1359142200
Option 1:
$date = date("Y-m-d", strtotime("25 Jan 2013 07:30 PM"));
$time = date("H:i:s", strtotime("25 Jan 2013 07:30 PM"));
Option 2
$datetime = DateTime::createFromFormat("j M Y H:i A", "25 Jan 2013 07:30 PM");
$date = $datetime->format("Y-m-d");
$time = $datetime->format("H:i:s");
For best results, convert $date to a DateTime object with DateTime::createFromFormat and then use DateTime::format to get the values for your separate fields.
Example:
$dt = DateTime::createFromFormat("d M Y H:i A", "25 Jan 2013 07:30 PM");
$date = $dt->format("Y-m-d");
$time = $dt->format("H:i:s");
So you just want them separated? Something like:
$year_month_day = date('Y/m/d', strtotime($date));
$timestamp = date('H:i:s', strtotime($date));
More info: http://www.php.net/manual/en/function.date.php
If you want it as a timestamp then strtotime() is the PHP function to use:-
http://php.net/manual/en/function.strtotime.php
If you want it in a MySQL date format string then strtotime() in combination with date() would be the answer like this:-
$db_date = date("Y-m-d H:i:s", strtotime($date) );

Categories