mazraara Posted September 30, 2013 Share Posted September 30, 2013 i have the below given time interval options in a dropdown (those are strings) <select name="CSSAtapsClient[client_time_window][0]" id="client_time_window_0"><option value="5702">7am - 10am</option><option value="5703">10am - 1pm</option><option value="5704">12pm - 3pm</option><option value="5705">3pm - 6pm</option><option value="5706">6pm - 9pm</option><option value="5707">7pm - 10pm</option><option value="5708">9pm - 12am</option><option value="5709">12am - 7am</option></select> I need to convert these intervals for a specific GMT time zone. for example lets say its in GMT +8 and i need to convert it to GMT +10 and it can be done by adding 2 hours. so if the given time interval is, 7am - 10am (GMT + it should come as 9am - 12pm (GMT +10) What is the best way to convert this kind of a time interval ? The issue i am seeing here is its a string (time interval). Appreciate an early reply. EDIT 1 I am converting to only Australian states so there is no chance of getting a day as difference when converting. pls check this link http://www.timebie.com/tz/australiatimezone.php Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/ Share on other sites More sharing options...
mazraara Posted September 30, 2013 Author Share Posted September 30, 2013 (edited) Found this answer... <?phpfunction is_am_pm($str){if(strpos($str, "am") !== false || strpos($str, "AM") !== false) return "am";if(strpos($str, "pm") !== false || strpos($str, "PM") !== false) return "pm";}function get_only_int($str){return (int) preg_replace('/\D/', '', $str);}function gmt_to_gmt($str,$from_gmt,$to_gmt = "GMT +10"){if(!is_numeric($from_gmt)) $from_gmt = get_only_int($from_gmt);if(!is_numeric($to_gmt)) $to_gmt = get_only_int($to_gmt);$temp_time = explode("-",$str);$begin_time_s = is_am_pm($temp_time[0]);$begin_time = get_only_int(trim($temp_time[0]));$end_time_s = is_am_pm($temp_time[1]);$end_time = get_only_int(trim($temp_time[1]));$time_diff = $to_gmt - $from_gmt;$begin_time = $begin_time + $time_diff;$end_time = $end_time + $time_diff;if($begin_time > 13){if($begin_time_s == "am"){$begin_time -= 12;$begin_time .= "pm"; }else{$begin_time -= 12;$begin_time .= "am"; }}else{$begin_time .= $begin_time_s;}if($end_time > 13){if($end_time_s == "am"){$end_time -= 12;$end_time .= "pm"; }else{$end_time -= 12;$end_time .= "am"; }}else{$end_time .= $end_time_s;}return $begin_time . " - " . $end_time;}echo gmt_to_gmt("7am - 11pm", "GMT +0" , "GMT +2");?> but its not displaying some parts correctly. when 7am - 11pm is given it should return 9am - 1pm but here its outputting 9am - 13pm Edited September 30, 2013 by mazraara Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/#findComment-1451856 Share on other sites More sharing options...
Ch0cu3r Posted September 30, 2013 Share Posted September 30, 2013 Change if($end_time > 13){ to if($end_time >= 13){ Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/#findComment-1451860 Share on other sites More sharing options...
mazraara Posted September 30, 2013 Author Share Posted September 30, 2013 (edited) Thanks a lot. but i am thinking why does it returns 12am - 1am when i input 10am - 11pm. Actually it should be 12 pm not 12 am Appreciate your help Edited September 30, 2013 by mazraara Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/#findComment-1451867 Share on other sites More sharing options...
Shareek Posted September 30, 2013 Share Posted September 30, 2013 bump Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/#findComment-1451868 Share on other sites More sharing options...
kicken Posted September 30, 2013 Share Posted September 30, 2013 Rather than trying to convert 7pm-11pm, why not just leave it as is? Something like 7pm is a relative time, there is no need to be trying to do timezone calculations on it. Leave 7pm as it is and you can handle timezone differences when you generate an absolute date later on. What exactly are you attempting to do? Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/#findComment-1451898 Share on other sites More sharing options...
Barand Posted September 30, 2013 Share Posted September 30, 2013 You could try this method $perth = new DateTimeZone('australia/perth'); $darwin = new DateTimeZone('australia/darwin'); $sydney = new DateTimeZone('australia/sydney'); $time = DateTime::createFromFormat('G:ia', '10:00am', $perth); echo $time->format('G:ia') . " Perth<br>"; $time->setTimezone($darwin); echo $time->format('G:ia') . " Darwin<br>"; $time->setTimezone($sydney); echo $time->format('G:ia') . " Sydney<br>"; /*** RESULTS ********** 10:00am Perth 11:30am Darwin 12:00pm Sydney ***********************/ Quote Link to comment https://forums.phpfreaks.com/topic/282563-time-or-time-interval-conversion-with-php/#findComment-1451940 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.