Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date() print 24:00 instead of 00:00

Tags:

date

php

PHP date("H:i (d.m.Y)",$timestamp) function represents exact midnight as 00:00 of following day. But I need it to represent it as 24:00 of preceding day. Is it possible to be done without writing completely new date() parser function?

edit: Why do I need such 'weird' format? Because my client demands it. In my country (CZ), 24:00 is sometimes used when referring to exact midnight.

edit2: My current 'dirty' solution is: (does not work with all possible format strings)

function date_24midnight($format,$ts)
{
   if(date("Hi",$ts)=="0000")
      return preg_replace('/23:59/',"24:00",date($format,$ts-1));
   else
      return date($format,$ts);
}
like image 453
David162795 Avatar asked Feb 24 '14 14:02

David162795


People also ask

How do I change from 24 hour format to 12 hour format in PHP?

php //current Date, i.e. 2013-08-01 echo date("Y-m-d"); //current Time in 12 hour format, i.e. 08:50:55pm echo date("h:i:sa"); //current Time in 24 hour format, i.e. 18:00:23 echo date("H:i:s"); //current Month, i.e. 08 echo date("m"); //current Month name, i.e. Aug echo date("M"); ?>

What does date () do in PHP?

PHP date() Function The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.

How can I get current date in YYYY MM DD format in PHP?

date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y"). "<br />"; echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />"; echo date("m/d/Y")."<br />"; echo "<strong>Display current date mm-dd-yyyy format </strong>".


1 Answers

function date_24midnight($format,$ts)
{
   if(date("Hi",$ts)=="0000") {
      $replace = array(
        "H" => "24",
        "G" => "24",
        "i" => "00",
      );

      return date(
        str_replace(
          array_keys($replace),
          $replace, 
          $format
        ),
        $ts-60 // take a full minute off, not just 1 second
      );
   } else {
      return date($format,$ts);
   }
}

This function is based on yours, But works for all formats (that I tested)

So H:i:s works for example

It will need some extra work to for for formats like "\H\i Y-m-d"

like image 100
exussum Avatar answered Oct 02 '22 13:10

exussum