Reputation: 212
I need to display current time and timezone in my wordpress site.
I know how to get current time. But I donot know how to get timezone.
I'm unable to use any plugin for this.
Somebody please help me to get timezone using php code
Upvotes: 0
Views: 7532
Reputation: 5060
/**
* This is how wordpress gets the current date and time
* as per the timezone setting used in the WP-Admin settings
* @see /wp-admin/options-general.php:160
*/
$now = date_create( date_i18n( 'Y-m-d H:i:s' ) );
Upvotes: 4
Reputation: 2272
You can echo the time zone like I have set the time zone to India
<?php
if(empty($_GET['useroffset'])){
echo '<SCRIPT Language="JavaScript">
var curDateTime = new Date()
document.write("GMT Offset for your time zone is ")
document.write(-(curDateTime.getTimezoneOffset()/60))
</SCRIPT>';
}
elseif(is_numeric($_GET['useroffset'])){
$useroffset = $_GET['useroffset'];
echo $useroffset;
}
?>
New EDIT on requirement
You need the time on the client which you can get from Javascript using:
d=new Date();
offset=d.getTimezoneOffset();
Which returns the users offset from GMT in minutes. Pass this to your PHP (AJAX) and use something like:
$zero = 0;
$timenow = date("D d M Y H:i:s");
$tserver = strtotime($timenow, $zero);
$timenow .= " UTC";
$tUTC = strtotime($timenow, $zero);
$diff = ($tserver - $tUTC)/60;
// echo("Client Difference:" . $offset . " Server Difference:" . $diff);
$offset = $diff - $offset;
This gives you the offset between the server and the client which you can then apply to your calculations.
Upvotes: 0
Reputation:
I'm using this code:
<?php
if (date_default_timezone_get()) {
echo 'Current Time in ' . date_default_timezone_get() . ':';
}
?>
Have a look at this
Upvotes: 1