17. October 2012
von Blackbam

As it was a bit tricky to find out how this works, I want to share this script. This is how you can convert a UTC timestamp without offset and summertime into a converted timestamp using PHP. This is especially useful when extending a Calendar Plugin for example, which saves its dates in UTC-timestamps while local time is needed.

/** Calculate middle european summertime */
function utc_to_local_time($timestamp,$offset=0,$summertime=true) { 
	 $year = strftime("%Y", $timestamp); 
	$timestamp += (3600*intval($offset)); // add local offset
	
	// Calculate beginning and end of Summertime
	$initTime = mktime(2,0,0,3,31-date('w', mktime(2,0,0,3,31,$year)),$year); 
	$endTime = mktime(2,0,0,10,31-date('w', mktime(2,0,0,10,31,$year)),$year); 

    // Check if summertime, return adequatly
    if (($timestamp > $initTime && $timestamp < $endTime) && $summertime) { 
        return ($timestamp + 3600); 
    } else { 
        return ($timestamp); 
    } 
}

// example: Austria time
echo date("d.m.Y, H:i",utc_to_local_time($utc_time,1);
Share

Warning: Undefined variable $time_since in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 42 Dieser Eintrag wurde am 17. October 2012 um 0:00 in der Kategorie PHP veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: ,

Fatal error: Uncaught Error: Undefined constant "Ext_related_links" in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php:75 Stack trace: #0 /home/.sites/609/site1266/web/blackbams-blog/wp-includes/template-loader.php(106): include() #1 /home/.sites/609/site1266/web/blackbams-blog/wp-blog-header.php(19): require_once('/home/.sites/60...') #2 /home/.sites/609/site1266/web/blackbams-blog/index.php(17): require('/home/.sites/60...') #3 {main} thrown in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75 internal_server_error <![CDATA[WordPress &rsaquo; Error]]> 500