Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to get current time in hour:minute:second?

Tags:

date

php

time

In order to get the date in the right format I want I used date("d-m-Y"). Now I want to get the time in addition to the date in the following format H:M:S How can I procede ?

like image 631
MarGa Avatar asked Dec 03 '12 15:12

MarGa


People also ask

How can I get hours minutes and seconds in PHP?

php //PHP program to convert seconds into //hours, minutes, and seconds $seconds = 6530; $secs = $seconds % 60; $hrs = $seconds / 60; $mins = $hrs % 60; $hrs = $hrs / 60; print ("HH:MM:SS-> " . (int)$hrs .

How can I get current time in hours and minutes in PHP?

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.

How do I get current time in PHP?

The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

What does NOW () return in PHP?

Definition and Usage. The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).


3 Answers

Anytime you have a question about a particular function in PHP, the easiest way to get quick answers is by visiting php.net, which has great documentation on all of the language's capabilities.

Looking up a function is easy, just visit http://php.net/<function name> and it will forward you to the appropriate place. For the date function, we'll visit http://php.net/date.

We immediately learn a couple things about this function by examining its signature:

string date ( string $format [, int $timestamp = time() ] )

First, it returns a string. That's what the first string in the above code means. Secondly, the first parameter is expected to be a string containing the format. There is an optional second parameter for passing in your own timestamp (to construct strings from some time other than now).

date("d-m-Y") // produces something like 03-12-2012

In this code, d represents the day of the month (with a leading 0 is necessary). m represents the month, again with a leading zero if necessary. And Y represents the full 4-digit year. All of these are documented in the aforementioned link.

To satisfy your request of getting the hours, minutes, and seconds, we need to give a quick look at the documentation to see which characters represents those particular units of time. When we do that, we find the following:

h   12-hour format of an hour with leading zeros    01 through 12
i   Minutes with leading zeros                      00 to 59
s   Seconds, with leading zeros                     00 through 59

With this in mind, we can no create a new format string:

date("d-m-Y h:i:s"); // produces something like 03-12-2012 03:29:13

Hope this is helpful, and I hope you find the documentation has benefiting to your development as I have to mine.

like image 117
Sampson Avatar answered Oct 19 '22 20:10

Sampson


You can combine both in the same date function call

date("d-m-Y H:i:s");  
like image 21
ThomasVdBerge Avatar answered Oct 19 '22 20:10

ThomasVdBerge


You can have both formats as an argument to the function date():

date("d-m-Y H:i:s")

Check the manual for more info : http://php.net/manual/en/function.date.php

As pointed out by @ThomasVdBerge to display minutes you need the 'i' character

like image 30
koopajah Avatar answered Oct 19 '22 21:10

koopajah