What is the best way to compare dates?

What is the best way to compare dates?

by Franc Pombal -
Number of replies: 4

Hello everyone ...

I am developing a function to my theme, and I walk around with a existential question...

What is the best way to compare dates? I have resolved the matter to compare start date with the current date, but I just do not like what I did ... strtotime($datestart)... $datestart <= $today... and blah! blah!...

I know, I know, with SQL... more blah! blah!... but that's not enough for me wink , because if we make changes in a multi-layer environment, we should not have to go down to work with SQL? Moreover, as I control the user did not write anything? ...

Suggestions welcome ... and discrepancies too!

 

P.S.: Yes, I have comprehensively searched... but not found any related documentation/post...

 

Average of ratings: -
In reply to Franc Pombal

Re: What is the best way to compare dates?

by Mary Evans -

You can only compare date/time stamps if they have been created.

So depends largely on what you are looking for to compare.

Things like Quizzes use timers, so perhaps ask Tim Hunt in General Developers forum? OR You could get really mathematical with this stuff!

http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html

Cheers

Mary

In reply to Franc Pombal

Re: What is the best way to compare dates?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Moodle stores all dates as integers (unix time-stamps). Just compare them with <. The bit that might be difficult is displaying them to users, but actually, for that, you just use the userdate() function.

In reply to Tim Hunt

Re: What is the best way to compare dates?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

To explain userdate():

To make things easy, all dates are converted into a number representing the number of seconds since the 00:00:00 1st January 1970 UTC - http://en.wikipedia.org/wiki/Unix_time.  Then date comparisons are simple as you only have to compare two numbers of the same unit and not work in 60's / 24's / 30's / 31's / 28's etc.  It also makes it possible to calculate durations and know if a given time is within a time window.

In reply to Franc Pombal

Re: What is the best way to compare dates?

by Franc Pombal -

Hi ...

Thanks Gareth, Tim and Mary ...

Yes, I had done a test with the dates, but as it worked on the first try... had any doubt... matter of trustmixed, I guess... Truly much simpler this way... 

I'll play with userdata() too... and design something to prevent the user to insert invalid dates...

I just have a 'sorrow'... not able to insert a date field in the theme admin page, like 'new admin_setting_dateselect' ... would be a good suggestion to implement??

Greetings, and thank you very much for such a quick response...