uno uno - 15 days ago 9
PHP Question

PHP: how to get custom date PHP 5.1.6

I have a code in php 5.1.6

which is:

$tz = date_default_timezone_set("Asia/Tokyo");
$date = date('2016-11-24 07:27:00 +00');
$date = date_default_timezone_get($tz);
$currentDate = date('F d, Y H:i');


I keep getting the current time on my output


November 24, 2016 16:09


How can I get


November 24, 2016 19:27


Thank you

Answer

Code below should help you:

date_default_timezone_set('Europe/London');
$datetime = new DateTime('2016-11-24 07:27:00');
$tokyo_time = new DateTimeZone('Asia/Tokyo');
$datetime->setTimezone($tokyo_time);
echo $datetime->format('F d, Y H:i');

If you want to add 3 hours you can use code below:

$datetime->add(new DateInterval('PT3H'));

So your code could look like follows:

date_default_timezone_set('Europe/London');
$datetime = new DateTime('2016-11-24 07:27:00');
$tokyo_time = new DateTimeZone('Asia/Tokyo');
$datetime->setTimezone($tokyo_time);
echo $datetime->format('F d, Y H:i');
$datetime->add(new DateInterval('PT3H'));

More informations can be found in manuals below:

PHP 5.1.6

If it doesnt work add those lines in your code:

$newdate = date('F d, Y H:i', strtotime($date.' + 3 hours'));
echo $newdate;

And your code would look as follows:

$tz = date_default_timezone_set("Asia/Tokyo");
$date = date('2016-11-24 07:27:00 +00');
$date = date_default_timezone_get($tz);
$currentDate = date('F d, Y H:i');
$newdate = date('F d, Y H:i', strtotime($date.' + 3 hours'));
echo $newdate;

More about used method you can read in PHP:strtotime