33

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

$date = "2011-07-26 20:05:00";

$hours = ?

8 Answers 8

66

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'
0
47

By hours I'm assuming you mean if the time is 8PM or 20:00 hours like it is in your time string then...

$date = "2011-07-26 20:05:00";
$date = strtotime($date);
echo date('H', $date);

I'm not that familiar with PHP's date formats so you'll have to reference PHP.net for the correct formatting character for that.

3
  • You will need to use strtotime() on $date to make it a unix timestamp before this will work...
    – 472084
    Jul 26, 2011 at 9:18
  • The first version actually returned an error because of the date formatting so use strtotime to reformat the date to a unix time stamp. Jul 26, 2011 at 9:19
  • 1
    Clement's answer below should really be the new accepted answer. Using the DateTime class would be the most reliable way to do this in 2017. strtotime can give weird results sometimes and you're giving up control where as DateTime::createFromFormat is much more reliable/consistent/predictable.
    – Wes
    Aug 31, 2017 at 21:03
1

At php side you use date('H',strtotime($date));

and at mysql side you can use DATE_FORMATE(NOW(),'%H') to get only hour

$date contains date like '2014/08/14' format

To convert date like '2014/08/14' to '14-08-2014' you have to use

date('d-m-Y',strtotime($date));

OR use

date('d-m-Y',strtotime(str_replace('/','-',$date)))
1

Best way to use DateTime class

$my_sql_date = "2011-07-26 20:05:00";
$date_time_obj = new DateTime($my_sql_date);
echo $date_time_obj->format('H');
1

For 24-hour and 12-hour format, please check below code:

    $date = "2011-07-26 20:05:00";
    echo $hours = date('G', strtotime($date)); // In 24-hour format of an hour without leading zeros
    echo '<br>';
    echo $hours = date('g', strtotime($date)); // 12-hour format of an hour without leading zeros
    // For current timestamp
    echo $hours = date('G'); // In 24-hour format of an hour without leading zeros
    echo '<br>';
    echo $hours = date('g'); //12-hour format of an hour without leading zeros

For reference please check: http://php.net/manual/en/function.date.php

1

The date_parse function will return a fulfilled array of a sections of a datetime. use it like this:

    $now = date(now()); // "2021-08-30 21:52:21"
    $open = date_parse($now)['hour']; // 21
2
  • 1
    Better to provide detailed explanation along with the code. Even if you think it can solve problem of the OP, your answer should be prepared such that it can be helpful for all people having such question. Please improve your answer.
    – akash
    Aug 31, 2021 at 3:00
  • 1
    Thank you for modifying my answer. I have just started to answer questions. I will take your point for next time.
    – Yuri
    Aug 31, 2021 at 15:57
0

$pattern = '[0-9]{2}:[0-9]{2}'; preg_match('/'.$pattern.'/', '2011-07-26 20:05:00', $matches); echo $matches[0];

1
  • 2
    Kindly explain it a bit as well. Thanks! Jul 24, 2018 at 4:53
0

As of CakePHP 4.0+ you have the FrozenTime utility class. Just import it.

In a controller:

use Cake\I18n\FrozenTime;
        
class ExampleController extends AppController {
    public function getHour() {
        $now = FrozenTime::now();
        $hour = $now->hour;
        $this->set(compact('hour'));
    }
}

Then in your view:

<?= $hour ?>

It should output something like this (assuming it's currently 2:30 PM):

14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.