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"); ?>
Output for 5.5.10 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) 1414728000
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.9
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) 1414724400
Output for 5.2.0 - 5.2.17
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) Fatal error: Call to undefined method DateTime::getTimestamp() in /in/6E0p9 on line 68
Process exited with code 255.
Output for 5.1.0 - 5.1.6
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) Fatal error: Class 'DateTimeZone' not found in /in/6E0p9 on line 60
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) Fatal error: Call to undefined function date_default_timezone_get() in /in/6E0p9 on line 57
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) Fatal error: Call to undefined function: date_default_timezone_get() in /in/6E0p9 on line 57
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.4
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) Fatal error: Call to undefined function: date_default_timezone_get() in /in/6E0p9 on line 57
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Array ( [start_timestamp] => 1414710000 [end_timestamp] => 1414796399 [start_iso] => 2014-10-31 00:00:00 [end_iso] => 2014-10-31 23:59:59 ) Fatal error: Call to undefined function: date_default_timezone_get() in /in/6E0p9 on line 57

preferences:
189.25 ms | 402 KiB | 219 Q