Calculate date (over a month)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Calculate date (over a month)

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Calculate date (over a month)

Post by Celauran »

Have you looked at DateTime::modify?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Calculate date (over a month)

Post by pickle »

Days over a month could be 29 -> infinity. Could we get some more details?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Re: Calculate date (over a month)

Post 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';  
	  }
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Calculate date (over a month)

Post 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').
Last edited by pickle on Fri Jun 08, 2012 9:42 am, edited 1 time in total.
Reason: Fixed broken list tag
Post Reply