Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Hours from Date in PHP & Cakephp?

Tags:

php

datetime

I want to get Only Hours From date by using PHP & Cakephp function.

$date = "2011-07-26 20:05:00";  $hours = ? 
like image 915
chetanspeed511987 Avatar asked Jul 26 '11 09:07

chetanspeed511987


People also ask

How can I get hours in PHP?

Try this: $hourMin = date('H:i'); This will be 24-hour time with an hour that is always two digits. For all options, see the PHP docs for date().

How can I get current date and time in PHP?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How get fetch time from database in PHP?

PHP time() Functionecho(date("Y-m-d",$t));

What is a timestamp in PHP?

A timestamp in PHP is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT). The value returned by the time function depends on the default time zone. The default time zone is set in the php.


1 Answers

Use the Datetime class (PHP 5.3 or higher).

$dt = DateTime::createFromFormat("Y-m-d H:i:s", "2011-07-26 20:05:00"); $hours = $dt->format('H'); // '20' 
like image 127
Clement Herreman Avatar answered Sep 29 '22 15:09

Clement Herreman