Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a given date is past

Tags:

date

php

time

I have a week calendar that holds events, and want that users can't add events for the past days. So I'm tring to use a function like that:

if( strtotime($this->day) < time() ){ // date format is YYYY-MM-DD
// date is past 
}else{   
// date is not past
}

It seems to works fine, except that it consider today date as a past day. What am i doing wrong?

like image 958
Luciano Avatar asked Jul 21 '10 09:07

Luciano


People also ask

How do you check if a date has passed JavaScript?

JavaScript Check if a Date is in the PastsetHours(0, 0, 0, 0)) { return true; } return false; }; var past = new Date('2020-05-20'); var today = new Date(); var future = new Date('2030-05-20'); dateInPast(past, today); dateInPast(future, today);

What does date getTime () do?

getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.

What does date now () mean?

The date. now() method is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. Since now() is a static method of Date, it will always be used as Date.


1 Answers

Simpler ->

if(strtotime($this->day) < strtotime(date('Y-m-d')))
{
   ...
}
else
{
   ...
}
like image 195
budinov.com Avatar answered Oct 14 '22 18:10

budinov.com