How get the current time?

How get the current time?

by Jorge San Martin -
Number of replies: 8
Hello,
I'm new here and I need some help.

I need get the current time, but using the date("Hi") function get one hour less. (at 12:00 I get 11:00)

I have the same problem with the chat module and I setup the timezone of my country and work fine.

Maybe I must use another date function to apply the timezone? What function it's used on the chat module to show the time? I don't find it.

Thank you very much

Jorge
Average of ratings: -
In reply to Jorge San Martin

Re: How get the current time?

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
$time = time(); // this will get you the current time in unix time format (seconds since 1/1/1970 GMT)

echo userdate($time, '%H:%M'); // this will print the time in the timezone of the current user (formats)
In reply to Martin Dougiamas

Re: How get the current time?

by Vitor Torres -
Hello Martin,

Do you know any Moodle function to give the current day and month?
Looking forward for an answer.

Peace,
Vitor
In reply to Vitor Torres

Re: How get the current time?

by Peter Bloomfield -
Hello Vitor,

Is there a particular reason you need to use a Moodle function? PHP has excellent date and time functions built-in (although it can take a while to get familiar with them). The date() function is very flexible, for example.

However, if you need the Moodle timezone, then the Moodle userdate() function already mentioned is ideal. The format string is the same as for PHP's strftime() function.

-Peter
In reply to Peter Bloomfield

Re: How get the current time?

by Shreyas Patankar -

Hello everyone,

I am new to php programming.Can we create a block on moodle which would display "good morning", "good evening" messages based on user's time.

Please help.


-Shreyas