tags:

views:

49

answers:

3

$today = time () ; this function is used to get current time of my system, i want to get South Africa time, so plz guide me.

+3  A: 

For example:

<?php
date_default_timezone_set('Africa/Johannesburg');
echo date('Y-m-d H:i:s', time());
Lauri Lehtinen
You should at least restore the timezone afterwards. See date_default_timezone_get
Artefacto
Thought about that too, but the question made me unsure .. maybe the South Africa time is desired across the whole site .. anyway, good point, up to @aamir
Lauri Lehtinen
+1  A: 

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.

Greg Hewgill
+1  A: 
$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";
Artefacto
me using it in a count down timer...its code:$date = explode(" ",$row['Date']);//print $date[0].'>>';list($y, $m, $d) = explode('-', $date[0]);list($h, $min, $sec) = explode(':', $date[1]);$target = mktime($h, $min, $sec, $m, $d, $y) ;$today = time () ;$difference =($target-$today) ; if ($difference < 0) $difference = 0;$days_left = floor($difference/60/60/24);$hours_left = floor(($difference - $days_left*60*60*24)/60/60);$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);Now guide me how cane me put this code.
Aamir
@aamir Fayyaz I've updated the answer.
Artefacto
@ Artefacto, by this code nothing displayed,
Aamir
is it possible ime can get the Number of Seconnds Since January 1 1970 00:00:00: (including leap year ) it will depend on timezone
Aamir
@aamir Fayyaz http://codepad.viper-7.com/m9joKr
Artefacto