/    Sign up×
Community /Pin to ProfileBookmark

Date Comparison

Kind of a dumb question…

Do you[I] have [/I]to convert a date to a timestamp in order to compare?

I have a while statement that runs until $startdate = $enddate, in this example I’ll use the following:

$startdate = 01/03/2006
$enddate = 01/10/2007

However when I run this it goes like this:

01/03/2006 – 01/10/2007
01/04/2006 – 01/10/2007
01/05/2006 – 01/10/2007
01/06/2006 – 01/10/2007
01/07/2006 – 01/10/2007
01/08/2006 – 01/10/2007
01/09/2006 – 01/10/2007
01/10/2006 – 01/10/2007

Then the while statement ends.

As you can see it’s startdate does not = enddate because it’s still a year back. I was impressed the comparison worked so well, until I noticed the years are not comparing.

Any thoughts advice?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehFeb 12.2007 — Do you[I] have [/I]to convert a date to a timestamp in order to compare?[/QUOTE]No, you could write your own function to do the comparison but it does seem sensible to use the built in ones.
Copy linkTweet thisAlerts:
@Doc_ThirstauthorFeb 12.2007 — Thanks man, I guess I just need to suck it up and convert it.

I guess I just don't understand why MS-SQL even has a time/date column type when you have to just convert everything to unix TS to actually use it.
Copy linkTweet thisAlerts:
@NightShift58Feb 13.2007 — It's not a MxSQL issue, really. More PHP... The DBMS' would know the difference.

I don't know how you are comparing, but it you're testing for equality (==), the year will be definitely be taken into consideration. If you're testing for greater-or-equal, you may run into problems because of the format of the date strings.

I guess the real question is: How are you incrementing your loop, from $startdate to ...? The simplest way would be to apply strtotime() on $startdate and $enddate, something like this:[code=php]<?php
$startdate = "01/03/2006";
$enddate = "01/10/2007";
$currentloopdate = strtotime($startdate);
$endloopdate = strtotime($enddate);
WHILE ($currentloopdate <= $endloopdate) :
$currentloopdate += 86400: // 24*60*60 = 1 day
ENDWHILE;[/code]
×

Success!

Help @Doc_Thirst spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...