Page 1 of 1

Calculate date (over a month)

Posted: Mon Jun 04, 2012 11:38 am
by D_zone
Hi everyone!

Can anybody give some tips, advices or ideas of how to calculate the days over a month.
I have a login system and a database but I don't want it to be specific with dates. the idea is having expressions like :
OVER A MONTH, OVER A WEEK, WITHING A MONTH etc.

I will truly appreciate it.

Re: Calculate date (over a month)

Posted: Mon Jun 04, 2012 11:44 am
by Celauran
Have you looked at DateTime::modify?

Re: Calculate date (over a month)

Posted: Mon Jun 04, 2012 2:03 pm
by pickle
Days over a month could be 29 -> infinity. Could we get some more details?

Re: Calculate date (over a month)

Posted: Mon Jun 04, 2012 6:15 pm
by D_zone
Yeah it worked ok with two weeks but It gets confusing when doing it a month since every month doesn't have exact amount of days (28,29,30,31)
I got this for within 2 weeks

Code: Select all


$day = '3'; //sample

$w_ago = strtotime("-7 day");
$week_ago = date("d", $w_ago);
$two_w_ago = strtotime("-14 day");
$two_week_ago = date("d", $two_w_ago);
 if($day < $week_ago && $day >= $two_week_ago){
		  echo '<strong>LAST LOGIN: </strong>'.'whitin 2 weeks';  
	  }

Re: Calculate date (over a month)

Posted: Fri Jun 08, 2012 7:49 am
by Chris Corbyn
I think your underlying logic is flawed. What your code says, to put it into plain English is:
  • Give me the day of the month 1 week ago
  • Give me the day of the month 2 weeks ago
  • Tell me if the day of the month when the user last logged in is between the day of the month 2 weeks ago and the day of the month last week
The problem with simply comparing a day of the month is that it is meaningless without the month and year. It would be much simpler to just compare UNIX timestamps, which are simple integer values.

Code: Select all

<?php

$last_login_time = strtotime('15 days ago'); // for example... presumably this actually comes from the database

if ($last_login_time > strtotime('2 weeks ago')) {
  echo '<strong>LAST LOGIN: </strong> within 2 weeks';  
}
I if you wanted to check if the user last logged in during the week before last, then add $last_login_time < strtotime('1 week ago').