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) );
Related
I am parsing a formatted date into another format like this...
$ordersDate = 'Saturday 8th of July 2017 21:22:52 PM';
$parsed = date_parse_from_format("l jS \of F Y H:i:s A", $ordersDate);
$new = mktime(
$parsed['hour'],
$parsed['minute'],
$parsed['second'],
$parsed['month'],
$parsed['day'],
$parsed['year']
);
$timestamp=$new;
$formatdate = gmdate("d/m/Y", $new);
echo $formatdate;
Why is this outputting ... 09/07/2017 and not 08/07/2017?
That is caused by you make wrong date. If you use AM/PM, just use 12 hours format. If you use 24 hours format.
$ordersDate = 'Saturday 8th of July 2017 21:22:52 PM';
21:22:52 PM will be equivalent with 33:22:52 and that's why the date become next date
I have a variable holding time in the format Apr 25, 2017 12:00:00 AM I wish to convert it to 2017-04-25 .My result should be without time
You can convert your string formatted date, to timestamp with strtotime() function. Then you can parameter date() function with converted timestamp, and format it with first parameter.
More about formatting:
http://php.net/manual/en/function.date.php
print date('Y-m-d',strtotime('Apr 25, 2017 12:00:00'));
You need to check the date method of PHP
$date = 'May 25, 2017 12:00:00 AM';
$date = date('Y-m-d', strtotime($date));
echo $date;
Try DateTime class:
$datestr = 'Apr 25, 2017 12:00:00 AM';
$date = DateTime::createFromFormat('M d, Y h:i:s A', $datestr);
echo $date->format('Y-m-d');
output
2017-04-25
Use the date() function:
$old_date="Apr 25, 2017 12:00:00 AM";
$new_date=date('Y-m-d', strtotime($old_date));
echo $new_date;
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?).
This question already has answers here:
php strtotime not working
(3 answers)
Closed 7 years ago.
below is my php code.
<?php
$date = "06 06 2015 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;
?>
Output is
1970-01-01 01:00:00
i need to 2015-06-06 6:05:00
Thanks
Try this
<?php
$date = "06 06 2015 6:05:00 am";
$datetime= \DateTime::createFromFormat("m d Y h:i:s a", $date);
$cdate = $datetime->format('Y-m-d H:s:i');
You want DateTime::createFromFormat() (http://php.net/manual/en/datetime.createfromformat.php). Using that you can convert a string to a datetime any way you want to.
If this is all you plan to do with dates, continue, otherwise, consider using a good date and time class like Carbon to do more with dates and generally save yourself a lot of effort.
$date = "06 06 2015 6:05:00 am";
$cdate = date_create_from_format('m d Y g:i:s a', $date);
This allows you to specify your own format using format strings. For the entire list of format strings see: http://php.net/manual/en/datetime.createfromformat.php
The resulting $cdate is a DateTime object and implements the DateTimeInterface (http://php.net/manual/en/class.datetimeinterface.php) so you can use the format() method to convert the output to any format you need.
For more on format(): http://php.net/manual/en/datetime.format.php
$date = "06 06 2015 6:05:00 am";
this format is not correct because first date or month but it will first year with the match of strtotime "Y-m-d H:s:i" and "06 06 2015" this is with space but it will be "-" sign.
$date = "2015-06-06 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;
How do I convert "2 October, 2012 12:28" to a comparable timestamp?
Goal is to get a timestamp value on which I can use comparable operators like <, >, = and !=.
Thanks for your help
You can try using DateTime::createFromFormat
$dateTime = DateTime::createFromFormat("d F, Y g:i", "2 October, 2012 12:28");
var_dump($dateTime->getTimestamp());
Output
int 1349173680
You can also add timezone
$dateTime = DateTime::createFromFormat("d F, Y g:i", "2 October, 2012 12:28", new DateTimeZone('UTC'));
var_dump($dateTime->getTimestamp());
Output
int 1349180880
You can get it to work with two calls to strtotime:
$date = strtotime("2 October, 2012");
// Now use the current date to get the time:
$timestamp = strtotime("12:28", $date);
You can separate the date segments with explode.