Home > Backend Development > PHP Tutorial > How to convert 12-hour clock to 24-hour clock in php_PHP tutorial

How to convert 12-hour clock to 24-hour clock in php_PHP tutorial

WBOY
Release: 2016-07-13 09:58:43
Original
1160 people have browsed it

How to convert 12-hour clock to 24-hour clock in php

This article describes the method of converting 12-hour clock to 24-hour clock in php. Share it with everyone for your reference. The details are as follows:

php converts 12-hour clock to 24-hour clock. The input format is: 02:30:00 pm converted to: 14:30:00

1

2

3

4

5

6

7

8

9

10

11

function to_24_hour($hours,$minutes,$seconds,$meridiem){

$hours = sprintf('d',(int) $hours);

$minutes = sprintf('d',(int) $minutes);

$seconds = sprintf('d',(int) $seconds);

$meridiem = (strtolower($meridiem)=='am') ? 'am' : 'pm';

return date('H:i:s', strtotime("{$hours}:{$minutes}:{$seconds}{$meridiem}"));

}

echo to_24_hour( 1, 2, 3, 'pm' ); // Returns 13:02:03

echo to_24_hour( '02', '30', '00', 'pm' ); // Returns 14:30:00

?>

1 2 3

4

6 7 8 9 10
11
<🎜>function to_24_hour($hours,$minutes,$seconds,$meridiem){<🎜> <🎜>$hours = sprintf(' d',(int) $hours);<🎜> <🎜>$minutes = sprintf(' d',(int) $minutes);<🎜> <🎜>$seconds = sprintf(' d',(int) $seconds);<🎜> <🎜>$meridiem = (strtolower($meridiem)=='am') ? 'am' : 'pm';<🎜> <🎜>return date('H:i:s', strtotime("{$hours}:{$minutes}:{$seconds}{$meridiem}"));<🎜> <🎜>}<🎜> <🎜>echo to_24_hour( 1, 2, 3, 'pm' ); // Returns 13:02:03<🎜> <🎜>echo to_24_hour( '02', '30', '00', 'pm' ); // Returns 14:30:00<🎜> <🎜>?>
http://www.bkjia.com/PHPjc/977166.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/977166.htmlTechArticleHow to convert 12-hour clock to 24-hour clock in php. This article explains how to convert 12-hour clock to 24-hour clock in php. Hourly method. Share it with everyone for your reference. The details are as follows: php will set the 12-hour format...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template