Ichibanj Posted May 5, 2020 Share Posted May 5, 2020 Hi there This code below displays the time in different timezones, from a base time of Los Angeles time, and it's working well for us (the time is pulled from the start date field of an event post type within Wordpress). However, if an event is at midday it puts 12:00 pm for the time which can cause confusion internationally as to whether it is midnight or midday. We'd like to display "12:00 noon" instead of "12:00 pm". Does anyone have any idea how this code could be adapted to do this? This code will be used in a Wordpress site so the way the time is displayed is set there. However, Wordpress doesn't have a way to change just this one things that we're looking to do which is to change '12:00 pm' to '12:00 noon'. Thanks! <?php $tas_start_datetime = tribe_get_start_date(); ?><strong>Los Angeles, USA: </strong><?php $date = new DateTime($tas_start_datetime, new DateTimeZone('America/Los_Angeles')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Detroit, USA: </strong><?php $date->setTimezone(new DateTimeZone('America/Detroit')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Accra, Ghana: </strong><?php $date->setTimezone(new DateTimeZone('Africa/Accra')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>London, UK: </strong><?php $date->setTimezone(new DateTimeZone('Europe/London')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Lagos, Nigeria: </strong><?php $date->setTimezone(new DateTimeZone('Africa/Lagos')); echo $date->format('l, F j\, g:i a') . "\n"; ?><br/><strong>Auckland, New Zealand: </strong><?php $date->setTimezone(new DateTimeZone('Pacific/Auckland')); echo $date->format('l, F j\, g:i a') . "\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/310702-if-echo-is-1200-pm-then-display-1200-noon-instead/ Share on other sites More sharing options...
requinix Posted May 5, 2020 Share Posted May 5, 2020 32 minutes ago, Ichibanj said: However, if an event is at midday it puts 12:00 pm for the time which can cause confusion internationally as to whether it is midnight or midday. I assume you're talking about the language barrier and people not knowing what "PM" means? But if that's the case then I wouldn't expect them to know what the word "noon" means either. As you've seen, neither PHP nor WordPress have something to print the time in the format you want. So you'll have to write some code to do it. Have you tried that yet? What did you come up with and how was it not working? Quote Link to comment https://forums.phpfreaks.com/topic/310702-if-echo-is-1200-pm-then-display-1200-noon-instead/#findComment-1577570 Share on other sites More sharing options...
ZulfadlyAshBurn Posted May 5, 2020 Share Posted May 5, 2020 (edited) You can implement a function to check if the time is 12:00 pm. If so, echo 12:00 noon. I've created a simple script. Hope it helps. function isNoon($format) { global $date; $timenow = date('g:i a'); if ($timenow == "12:00 pm") { echo date('l, F j\,') . " 12:00 noon"; } else { return $date->format($format); } } To adapt this into your code, you can try this. <?php $tas_start_datetime = tribe_get_start_date(); function isNoon($format) { global $date; $timenow = date('g:i a'); if ($timenow == "12:00 pm") { echo date('l, F j\,') . " 12:00 noon"; } else { return $date->format($format); } } ?><strong>Los Angeles, USA: </strong><?php $date = new DateTime($tas_start_datetime, new DateTimeZone('America/Los_Angeles')); echo isNoon('l, F j\, g:i a') . "\n"; ?><br/><strong>Detroit, USA: </strong><?php $date->setTimezone(new DateTimeZone('America/Detroit')); echo isNoon('l, F j\, g:i a') . "\n"; ?><br/><strong>Accra, Ghana: </strong><?php $date->setTimezone(new DateTimeZone('Africa/Accra')); echo isNoon('l, F j\, g:i a') . "\n"; ?><br/><strong>London, UK: </strong><?php $date->setTimezone(new DateTimeZone('Europe/London')); echo isNoon('l, F j\, g:i a') . "\n"; ?><br/><strong>Lagos, Nigeria: </strong><?php $date->setTimezone(new DateTimeZone('Africa/Lagos')); echo isNoon('l, F j\, g:i a') . "\n"; ?><br/><strong>Auckland, New Zealand: </strong><?php $date->setTimezone(new DateTimeZone('Pacific/Auckland')); echo isNoon('l, F j\, g:i a') . "\n"; ?> Edited May 5, 2020 by ZulfadlyAshBurn change time Quote Link to comment https://forums.phpfreaks.com/topic/310702-if-echo-is-1200-pm-then-display-1200-noon-instead/#findComment-1577572 Share on other sites More sharing options...
Ichibanj Posted May 5, 2020 Author Share Posted May 5, 2020 (edited) 42 minutes ago, requinix said: I assume you're talking about the language barrier and people not knowing what "PM" means? But if that's the case then I wouldn't expect them to know what the word "noon" means either. It's in case some people think 12:00 pm means midnight instead of noon, and since the events are online and around the world we're worried about people getting confused. Edited May 5, 2020 by Ichibanj clarification Quote Link to comment https://forums.phpfreaks.com/topic/310702-if-echo-is-1200-pm-then-display-1200-noon-instead/#findComment-1577573 Share on other sites More sharing options...
Ichibanj Posted May 6, 2020 Author Share Posted May 6, 2020 The confusion is that some people will think 12:00pm means noon and some people will think it is midnight. It's one of those things that people debate, but regardless of what is correct some people may get confused. I was hoping it would be some kind of simple if statement squeezed into this code. I'm not a php coder, it took me a full day to produce the code I showed and even then it felt like a miracle I got it working! Thanks @ZulfadlyAshBurn for that but unfortunately that resulted in a fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/310702-if-echo-is-1200-pm-then-display-1200-noon-instead/#findComment-1577596 Share on other sites More sharing options...
ZulfadlyAshBurn Posted May 6, 2020 Share Posted May 6, 2020 2 hours ago, Ichibanj said: The confusion is that some people will think 12:00pm means noon and some people will think it is midnight. It's one of those things that people debate, but regardless of what is correct some people may get confused. I was hoping it would be some kind of simple if statement squeezed into this code. I'm not a php coder, it took me a full day to produce the code I showed and even then it felt like a miracle I got it working! Thanks @ZulfadlyAshBurn for that but unfortunately that resulted in a fatal error. My mistake, added another echo instead of return on the function. Quote Link to comment https://forums.phpfreaks.com/topic/310702-if-echo-is-1200-pm-then-display-1200-noon-instead/#findComment-1577600 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.