tanya
tanya

Reputation: 301

Extract time from a date/time variable PHP

I have an external variable coming in a file in the following format:

<?php  $var = JHtml::_('date', $item->event_date, this->config->event_date_format, null) ;  ?>

which outputs something like this:

09-05-2015 6:00 am

How can I remove the date part and keep only the hours?

Upvotes: 0

Views: 743

Answers (3)

Saty
Saty

Reputation: 22532

Use strtotime and date() for this

 $date = '09-05-2015 6:00 am';
 echo $time=date("g", strtotime($date));

OUTPUT

6

Upvotes: 1

veta
veta

Reputation: 76

the easiest way is to use substr:

substr($var, -7);

Upvotes: 0

Ruben Buijs
Ruben Buijs

Reputation: 16

This should do the trick. strtotime() can transform this date into a php-readable date. You can choose which items you want to show (hour, seconds, day, etc). http://php.net/manual/en/function.date.php#format

date('H:s', strtotime($var));

Upvotes: 0

Related Questions