PHP Convert date using strtotime? [duplicate] - php

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;

Related

convert date August 22, 2016 to 08/22/2016? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have this date format: August 22, 2016
I want to convert to 08/22/2016
str_replace is the solution?
eg: str_replace("August","08/",$var);
but it should have at least 12 str_replaces...
any ideas for a simple way?
Refer to DateTime::format
Specify your desired date format 'm/d/Y' after creating the DateTime object.
Try this:
$date = new DateTime('August 22, 2016');
echo $date->format('m/d/Y'); // 08/22/2016
try this,
$originalDate = "August 22, 2016";
$newDate = date("m/d/Y", strtotime($originalDate));
echo $newDate;
OUTPUT
08/22/2016
DEMO
just like php date function convert to in strtotime,
Example:
$date = "August 22, 2016";
$date2 = date("m/d/Y", strtotime($date));
echo $date2;
ANS : 08/22/2016
Note: if you change date format so change in date function so u like

PHP date format from a date format like "23 Nov, 2015 16:44:26 GMT" [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I am doing scraping and I got a date like 23 Nov, 2015 16:44:26 GMT. I would like to convert it to Y-m-d H:i:s so that I can save it to database datetime field.
I tried like this:
echo date("Y-m-d H:i:s", strtotime("23 Nov, 2015 16:44:26 GMT"));
I have also removed GMT by preg_replace and then
echo date("Y-m-d H:i:s", strtotime("23 Nov, 2015 16:44:26"));
It's giving 1970-01-01 01:00:00
You can use DateTime::createFromFormat to convert string to DateTime object.
http://php.net/manual/en/datetime.createfromformat.php
You can create a DateTime using DateTime::createFromFormat.
For the 'GMT' part from your date, you can use 'T' in the format for the Timezone abbreviation.
$dateTime = DateTime::createFromFormat('d M, Y H:i:s T', '23 Nov, 2015 16:44:26 GMT');
echo $dateTime->format('Y-m-d H:i:s');
Will result in:
2015-11-23 16:44:26
Demo
try this
<?php
$datestr = "23 Nov, 2015 16:44:26 GMT";
$datestr = str_replace(",", "", $datestr);
$dateArray = explode(" ", $datestr) ;
unset($dateArray[count($dateArray)-1]);
$newDateStr = '';
$i=0;
foreach ($dateArray as $dateArrayValue)
{
$hyphenStr = " ";
if($i>0 && $i != 3)
{
$hyphenStr = "-";
}
$newDateStr .= $hyphenStr.$dateArrayValue ;
$i++;
}
$newDateStr = trim($newDateStr);
echo date("Y-m-d H:i:s", strtotime($newDateStr));
?>
Also visit : http://php.net/manual/en/function.strtotime.php

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) );

PHP: Simple way to convert "Oct 24, 2011" date to "2011-10-24"? [duplicate]

This question already has answers here:
php convert date to MySQL date
(4 answers)
Closed 9 years ago.
I need to convert Oct 24, 2011 03:16 PM to 2011-10-24 15:16:00 in php.
I know its possible by creating a variable for each month "$jan = 01, $feb = 02, etc" and then using "explode" to split the information by space and re-arranging it in the correct order.
But is there any simpler way to do it?
$string_date = strtotime("Oct 24, 2011");
$date_object = strtotime($string_date);
$formatted = date("Y-m-d h:i:s", $date_object);
Check out the date() function for more formatting options.
The function strtotime() is a very powerful date parser. Use it to get a UNIX timestamp of the incoming date, and use the date() function to output the date in your desired format.
<?php
$ts = strtotime("Oct 24, 2011 03:16 PM");
$date = date("Y-m-d H:i:s", $ts);
?>
echo date("Y-m-d H:i:s", strtotime("Oct 24, 2011 03:16 PM"));
date( 'Y-m-d H:i:s' , strtotime ("Oct 24, 2011 03:16 PM" ) );
The strtotime() option mentioned in other answers here is the easiest method, but don't fall in love with the function for ALL date string->number conversions. It can and WILL screw up at some point. If you know exactly what format a date string will be in, then use the more reliable
$date = DateTime::createFromFormat('M j\, Y', "Oct 24,2011");

Categories