3v4l.org

run code in 300+ PHP versions simultaneously
<?php function get_date_range_from_time_period($str_time_period,$str_base_time = NULL) { if (!empty($str_base_time)) { $int_current_time = $str_base_time; } else { $int_current_time = time(); } $arr_parameter_range = array(); if ($str_time_period == "yesterday") { $int_start_time = mktime(0,0,0,date("m",$int_current_time),date("d",$int_current_time)-1,date("Y",$int_current_time)); $int_end_time = mktime(23,59,59,date("m",$int_current_time),date("d",$int_current_time)-1,date("Y",$int_current_time)); } else if ($str_time_period == "last_week") { // trickiest - we start by finding the current day of the week which we can use as an offset $int_current_day_of_week = date("N",$int_current_time); $int_week_start_offset = 6 + $int_current_day_of_week; $int_week_end_offset = $int_week_start_offset - 6; $int_start_time = mktime(0,0,0,date("m",$int_current_time),date("d",$int_current_time)-$int_week_start_offset,date("Y",$int_current_time)); $int_end_time = mktime(23,59,59,date("m",$int_current_time),date("d",$int_current_time)-$int_week_end_offset,date("Y",$int_current_time)); } else if ($str_time_period == "last_month") { $int_start_time = mktime(0,0,0,date("m",$int_current_time)-1,1,date("Y",$int_current_time)); $int_end_time = mktime(23,59,59,date("m",$int_current_time),0,date("Y",$int_current_time)); // 0 date should make it return the last day of the previous month } else { return FALSE; } $arr_parameter_range['start_timestamp'] = $int_start_time; $arr_parameter_range['end_timestamp'] = $int_end_time; $arr_parameter_range['start_iso'] = date("Y-m-d H:i:s",$int_start_time); $arr_parameter_range['end_iso'] = date("Y-m-d H:i:s",$int_end_time); return $arr_parameter_range; } function convertUnixTimeBetweenTimezones($vUnixTime,$vOriginalTimezone,$vDesiredTimezone) { if ($vOriginalTimezone == $vDesiredTimezone) { // no timezone shifting required return $vUnixTime; } // set default to desired timezone $vCurrentTimezone = date_default_timezone_get(); date_default_timezone_set($vDesiredTimezone); $oBaseTimeZone = new DateTimeZone($vOriginalTimezone); // user time zone $oCurrentTimeZone = new DateTimeZone($vDesiredTimezone); $vIsoTime = date("Y-m-d H:i:s",$vUnixTime); $oBaseTime = new DateTime($vIsoTime,$oBaseTimeZone); $oBaseTime->setTimezone($oCurrentTimeZone); $vTimeStamp = $oBaseTime->getTimestamp(); date_default_timezone_set($vCurrentTimezone); return $vTimeStamp; } $arr_data = get_date_range_from_time_period('yesterday',strtotime("2014-11-01 00:00:00")); print_r($arr_data); echo convertUnixTimeBetweenTimezones($arr_data['start_timestamp'],"EST","UTC"); ?>

preferences:
31.01 ms | 402 KiB | 5 Q