This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
how can I format datetime in php from: Tue Dec 09 2014 12:00:00 GMT+0200 (FLE Standard Time) to: 2014-12-09 12:00?
Thank you.
Problem solved. I did it by myself:
$datep = $_POST["datetimepicker"];
$placeholders = array('(FLE', 'Standard', 'Time)');
$vals = array('', '', '');
$str = str_replace($placeholders, $vals, $datep);
$newDate = date('Y/m/d H:i', strtotime($str));
echo $newDate;
One method is to use the datetime class:
$dateTime = new DateTime($yourDateTimeVariable);
$dateTime = $dateTime->format('Y-m-d H:i');
echo $dateTime;
Try this -
$date = "Tue Dec 09 2014 12:00:00 GMT+0200";
$newDate = date('Y-m-d H:i', strtotime($date));
You can use strototime combined with date. Something like
$date = "Tue Dec 09 2014 12:00:00 GMT+0200 (FLE Standard Time)";
$format = date("Y-m-d H:i", strtotime($date));
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Need a little help.
I'm using PHP.
Here is a example date:
Mon Sep 05 2016 15:30:00 GMT+0800 (China Standard Time)
What i want is display a outpout like this:
2016-09-05 15:30:00
I've tried to convert it using this function:
date('Y-m-d h:i:s', strtotime($time));
But the output is:
1970-01-01 08:00:00
Need some advice
Thanks.
Just use DateTime():
$time = 'Mon Sep 05 2016 15:30:00 GMT+0800';
$date = new DateTime($time);
echo $date->format('Y-m-d H:i:s');
<?php
$timezone = -5; //(GMT -5:00) EST (U.S. & Canada)
echo gmdate("Y-m-j H:i:s", time() + 3600*($timezone+date("I")));
?>
This doc would help more
http://php.net/manual/en/function.gmdate.php
$date=date_create("Mon Sep 05 2016 15:30:00 GMT+0800");
echo date_format($date,"Y/m/d H:i:s");
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
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;
I have a string in this format:
Thu, 26 Feb 2015 11:39:59
And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.
So ideally in the end I would have something like:
2015-03-26 11:39:59
Is there a function in PHP to do something like this?
Use DateTime() to format date string.
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = new DateTime($date);
echo $date->format('Y-m-d H:i:s');
You can also use it as DateTime::createFromFormat
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59
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) );