Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get South Africa Standard Time In Php

Tags:

php

this function is used to get current time of my system, I want to get South Africa time, so plz guide me.

$today = time () ;
like image 435
Amir Avatar asked Jun 07 '10 07:06

Amir


3 Answers

For example:

<?php
date_default_timezone_set('Africa/Johannesburg');
echo date('Y-m-d H:i:s', time());
like image 193
Lauri Lehtinen Avatar answered Oct 21 '22 04:10

Lauri Lehtinen


$d = new DateTime("now", new DateTimeZone("Africa/Johannesburg"));
echo $d->format("r");

gives

Mon, 07 Jun 2010 02:02:12 +0200

You can change the format. See http://www.php.net/manual/en/function.date.php

time() gives the number of seconds since January 1 1970 00:00:00 GMT (excluding leap seconds), so it doesn't depend on the timezone.

EDIT: For a countdown, you can do:

$tz = new DateTimeZone("Africa/Johannesburg");
$now = new DateTime("now", $tz);
$start = new DateTime("2010-06-11 16:00:00", $tz);
$diff = $start->diff($now);
echo "Days: " . $diff->format("%d") . "\n";
echo "Hours: " . $diff->format("%h") . "\n";
echo "Minutes: " . $diff->format("%i") . "\n";
echo "Seconds: " . $diff->format("%s") . "\n";
like image 27
Artefacto Avatar answered Oct 21 '22 05:10

Artefacto


The time() function returns the same value all around the world. Its return value is not dependent on the local time zone.

To convert a time value to your local time, call the localtime() function.

like image 44
Greg Hewgill Avatar answered Oct 21 '22 04:10

Greg Hewgill