Jump to content

If echo is '12:00 pm' then display '12:00 noon' instead


Ichibanj

Recommended Posts

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";
?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by ZulfadlyAshBurn
change time
Link to comment
Share on other sites

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 by Ichibanj
clarification
Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.