PHP - Convert string to date in - php

I am getting a string like Wed Sep 28 2016 01:00:00 GMT+0500 (PKT) and I need to convert it to 2016-09-28 01:00:00
I have tried this
$startTime = strtotime($updatedData['start']);
echo $time = date("Y-m-d H:i:s",$startTime);
but it returns me 2016-09-27 20:00:00

You could change it to use DateTime:
$startTime = new DateTime('Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)');
echo $startTime->format('Y-m-d H:i:s');
DateTime keeps the timezone you give him.
Live Example: https://3v4l.org/UTltO

#copynpaste solution is nice and straight forward but I will still share my solution by using Carbon.
Carbon is a library included together with laravel and here is the
documentation.
$carbon = new Carbon('Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)');
$carbon->format('Y-m-d H:i:s');
echo $carbon;
it will come out the result same as DateTime
2016-09-28 01:00:00
So what carbon nice is you can just add day, minute, second and etc by just a very minimal code, here is an example:
$carbon->addDays(1);
echo $carbon;
//result
2016-09-29 01:00:00

Try this:
$startTime = strtotime($updatedData['start']);
$time = date("Y-m-d H:i:s",$startTime);
echo date( "Y-M-d H:i:s", strtotime( $time) + 5 * 3600 );
It returns UTC time and you do need to add 5 hours to it. Also a quick suggestion. You can use Carbon for handling the date time.

You can set the desired time zone before converting. Please see the below code as a reference.
date_default_timezone_set("Asia/Bangkok");
$str = 'Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)';
$startTime = strtotime($str);
echo $time = date("Y-m-d H:i:s",$startTime);

Related

Convert a datepicker string into simple date format in php

My vuexy datepicker returns the following string:
Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)
In my controller (Laravel) I want to convert this into simple date format - yyyy-mm-dd.
I have tried with strtotime() & date() but nothing works. It may need to use DateTime::createFromFormat() but I could not pick the syntax.
How can I convert it into yyyy-mm-dd?
This is working for me:
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date); # Get rid of TZ from the date string
echo date('Y-m-d', strtotime($date));
Here's another sample using the DateTime class
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$date = preg_replace('/\(.*\)$/', '', $date);
$dt = new DateTime($date);
echo $dt->format('Y-m-d');
Both Output:
2020-12-01
Hi Friend You Can Use Carbon Package one of the most recomonded package. Please follow the code as below
use Carbon; //use at the begining of class/controller
$date = 'Tue Dec 01 2020 21:30:00 GMT+0600 (Bangladesh Standard Time)';
$formated_date = Carbon::parse($date)->format('Y-m-d');
//output will be 2020-12-01

PHP Change 24 hour datetime using strtotime

I am using strtotime for saving date like strtotime($_POST['end_date']) and this give me format 1441785600 that show very fine in google calendar but when I save string like
Thu Sep 03 2015 14:00:00 (24 hour format)
Data stop displaying in google calendar, I want to convert above sample (24Hours) using strtotime and convert it into something like that 1441785600
You may use something like this which currently doesn't give as you want, but may be useful-
$date = 'Sep 03 2015 14:00:00';
$parsedDate = date_parse_from_format('M d Y H:i:s', $date);
$time_stamp = mktime(
$parsedDate['hour'], $parsedDate['minute'], $parsedDate['second'],
date('n', strtotime($parsedDate['month'])), $parsedDate['day'], $parsedDate['year'],
date('I')
);
You could do:
$dateString = 'Thu Sep 03 2015 14:00:00 (24 hour format)';
echo strtotime(preg_replace('~\w+\s(.+)\s\(.+~','\1',trim($dateString)));

Transform date to valid format for PostgreSQL with PHP

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

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

Javascript time to PHP time

I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

Categories