PWaddict

How to remove the seconds from %X time format?

8 posts in this topic

I'm using the following method to display the time based on each locale:

<?= strftime("%X", $page->getUnformatted("tour_date")) ?>

Here are the results:

Locale en_US.utf8 = 12:00:00 AM
Locale es_ES.utf8 = 00:00:00
Locale el_GR.utf8 = 12:00:00 πμ

How can I remove the seconds (:00) ?

Share this post


Link to post
Share on other sites

Here is the solution:

<?php
$oldtime = strftime("%X", $page->getUnformatted("tour_date"));
echo $newtime = substr($oldtime, 0, strpos($oldtime, ':', strpos($oldtime, ':')+1)) . " " . strftime("%p", $page->getUnformatted("tour_date"));
?>

Now I'm getting the time formats based on each locale without the seconds:

Locale en_US.utf8 = 12:00 AM
Locale es_ES.utf8 = 00:00
Locale el_GR.utf8 = 12:00 πμ

If you know a better method please feel free to share it!

1 person likes this

Share this post


Link to post
Share on other sites

You can use the format "%l:%M %p" to display the time, without seconds. For example:

$p = wire("pages")->get(some_id);
echo strftime( "%l:%M %p",$p->getUnformatted($p->created));

returns a time like, 6:00 AM.

I apologize if I am misunderstanding what you are wanting to achieve.

Share this post


Link to post
Share on other sites
15 minutes ago, rick said:

You can use the format "%l:%M %p" to display the time, without seconds. For example:


$p = wire("pages")->get(some_id);
echo strftime( "%l:%M %p",$p->getUnformatted($p->created));

returns a time like, 6:00 AM.

I apologize if I am misunderstanding what you are wanting to achieve.

That is very specific. It only displays the 12-hour format. My method displays the time based on each locale and without the seconds. For example some countries have 24-hour format and others the 12-hour etc.

1 person likes this

Share this post


Link to post
Share on other sites

Ah, ok. I knew strftime was locale aware, but not that it took into account the 12/24 hour format. Good to know.

Share this post


Link to post
Share on other sites
50 minutes ago, rick said:

Ah, ok. I knew strftime was locale aware, but not that it took into account the 12/24 hour format. Good to know.

You can check all the time formats for strftime here.

1 person likes this

Share this post


Link to post
Share on other sites
14 hours ago, PWaddict said:

 


<?php
$oldtime = strftime("%X", $page->getUnformatted("tour_date"));
echo $newtime = substr($oldtime, 0, strpos($oldtime, ':', strpos($oldtime, ':')+1)) . " " . strftime("%p", $page->getUnformatted("tour_date"));
?>

 

Unfortunately this code isn't working for all locales cause for example in Finland they use dots (.) instead of colons (:). I will check all the locales and adjust the code soon.

Share this post


Link to post
Share on other sites

The %X format doesn't work very well for some locales in a combination with %p format but there is a far better and easier way to get the correct time format without the seconds on each locale.

With the following method you can control all of the time outputs from all your datetime fields from just 1 field under each language!

  • Create a text field and name it: time_format.
  • Add the time_format field on the system's language template (Setup > Templates > Filters > Show system templates).
  • Go to each of your languages under Setup > Languages and add their strftime format on the time_format field. For example if the language locale is en_US.utf8 then the time format should be %l:%M %P if the locale is fi_FI.utf8 the time format should be %H.%M and so on.
  • Add the following code to your _init.php:
    $time_format = $user->language->time_format;

    and add this code on your templates you want to output the time from your datetime field:

    <?= strftime($time_format, $page->getUnformatted("your_datetime_field")) ?>

    DONE :)

     

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.