0

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

4
  • This is because of the timezone, the time is +5 but the result is +0. Commented Sep 28, 2016 at 5:46
  • check your timezone Commented Sep 28, 2016 at 5:46
  • Your out put date is in GMT. So please set the time_zone and run the script Commented Sep 28, 2016 at 5:46
  • set the timezone before start converting. date_default_timezone_set("Asia/Dili"), and convert using the format. For the format use Ex: DateTime::createFromFormat('j-M-Y', '15-Feb-2009'); echo $date->format('Y-m-d'); Commented Sep 28, 2016 at 6:11

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I needed
3

@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

1 Comment

+1 I like this answer better as the question is tagged laravel and this answer gives a way to do it using carbon.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.