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
WBOYOriginal
2016-07-13 09:58:431162browse

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...
Statement๏ผš
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