Getting Hour and Minute in PHP

202,784

Solution 1

print date('H:i');
$var = date('H:i');

Should do it, for the current time. Use a lower case h for 12 hour clock instead of 24 hour.

More date time formats listed here.

Solution 2

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().

Solution 3

print date('H:i');

You have to set the correct timezone in php.ini .

Look for these lines:

[Date]

; Defines the default timezone used by the date functions

;date.timezone =

It will be something like :

date.timezone ="Europe/Lisbon"

Don't forget to restart your webserver.

Solution 4

Another way to address the timezone issue if you want to set the default timezone for the entire script to a certian timezone is to use date_default_timezone_set() then use one of the supported timezones.

Solution 5

function get_time($time) {
    $duration = $time / 1000;
    $hours = floor($duration / 3600);
    $minutes = floor(($duration / 60) % 60);
    $seconds = $duration % 60;
    if ($hours != 0)
        echo "$hours:$minutes:$seconds";
    else
        echo "$minutes:$seconds";
}

get_time('1119241');
Share:
202,784
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to get the current time, in Hour:Min format can any one help me in this.

  • Admin
    Admin over 14 years
    Thanks a lot. but its not showing my system time.. there is some difference.
  • dcousineau
    dcousineau over 14 years
    This is deprecated. You need to set your timezone in the actual PHP script (there's a strict error thrown when you don't). php.net/date-default-timezone-set is the function you should use when setting your timezone in your PHP script.
  • Rob
    Rob over 14 years
    The output from date() will show the local time of the server. You may need to perform timezone adjustments, or else override the server's time zone as mentioned in other posts. Typically, your best bet is to deal with UTC/GMT timestamps (gmdate() et al) and make timezeone adjustments based off GMT, since it's portable across more configurations.
  • RozzA
    RozzA over 7 years
    and here i was looking for something similar to js getHour(). doh - of course!
  • Philipp Maurer
    Philipp Maurer over 5 years
    Hello Caleb, welcome to StackOverflow :-) We are glad about everyone that tries to help. Your answer to this question was yet given multiple times already and is formulated in a way that makes it look like a "Thank you" message. To keep the plattform clean, posting such messages as answers is not wanted.