72

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

ashleedawg
  • 20,365
  • 9
  • 72
  • 105

8 Answers8

145
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.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
MattBelanger
  • 5,280
  • 6
  • 37
  • 34
  • Thanks a lot. but its not showing my system time.. there is some difference. –  Oct 06 '09 at 14:31
  • 2
    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. – Rob Oct 06 '09 at 21:38
  • and here i was looking for something similar to js `getHour()`. doh - of course! – RozzA Aug 04 '16 at 21:10
21

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

Lucas Oman
  • 15,597
  • 2
  • 44
  • 45
12
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.

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
fernando
  • 169
  • 3
  • 1
    This is deprecated. You need to set your timezone in the actual PHP script (there's a strict error thrown when you don't). http://php.net/date-default-timezone-set is the function you should use when setting your timezone in your PHP script. – dcousineau Oct 06 '09 at 18:45
3

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.

Brooke.
  • 3,691
  • 13
  • 49
  • 80
2
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');
Abdo-Host
  • 2,470
  • 34
  • 33
1

In addressing your comment that you need your current time, and not the system time, you will have to make an adjustment yourself, there are 3600 seconds in an hour (the unit timestamps use), so use that. for example, if your system time was one hour behind:

$time = date('H:i',time() + 3600);

GSto
  • 41,512
  • 37
  • 133
  • 184
1

You can use the following solution to solve your problem:

echo date('H:i');
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
  • 1
    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. – Philipp Maurer Sep 21 '18 at 14:42
0
<?php
    date_default_timezone_set('Asia/Qatar');
    $DateAndTime = date('m-d-Y h:i:s a', time());  
    echo "<h3>The current date and time are <h3 style='color:B-G'> $DateAndTime.</h3></h3>";
    ?>
    
Ryan M
  • 18,333
  • 31
  • 67
  • 74
houari
  • 1