简体   繁体   中英

Convert timestamp into hour with PHP

Hi I am using following way to convert timestamp into hour:

 echo time() / 3600 % 24;

This return like 7 if its 7 GTM but I need its output such as 07:00

Please suggest.

Thanks!

you can do this with gmdate function

echo gmdate("H:i", time());

if you want only hours than just have only H in gmdate

gmdate — Format a GMT/UTC date/time

Demo

As @Mark Baker suggest if you want fix second timer than you can manually write like

echo gmdate("H:00", time());

You can use PHP's date function.

echo date('h:i', time());

Or, if you want it to always be :00 you can just do:

echo date('h', time()) . ':00';

If you want it to always be GMT, then you can use gmdate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM