Jump to content

Recommended Posts

Hi guys, I've hit a brick wall here and am in need of your help. :(

 

I'm pretty new to PHP and have limited knowledge to say the least. I'll explain what it is I'm trying to do.

 

Set start date as 01/01/2004 (dmY) $oFour

Set how many days has it been since then? $today

Set how many days it was from $ofour 30 days ago. $today -30 = $thirtyDaysAgo

 

 

But the problem is I don't know how to make

date('z');

work from 2004 and not 01/01/2010.

 

So $today will be how many days it has been since the start of 2004 and $thirtyDaysAgo will be $today -30. I can set up $thirtyDaysAgo no problem but it's just finding out how to get the $today number...

 

 

Hope anyone can offer a little light to my situation :/

 

Mav

 

 

Link to comment
https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/
Share on other sites

Check out the DateTime class

 

<?php
$date = '2004/10/21'; //get the start date from a form $_POST['date']

$oFour = new DateTime($date);
$today = new DateTime();

$date_of_oFour = $oFour->format('m-d-Y'); //accepts any string that date accepts.

$interval = $oFour->diff($today);
$thirty_days_from_oFour = $oFour->sub(new DateInterval('P30D'));

$days_since_oFour = $interval->format('%a days');
$days_subtracted_from_oFour = $oFour->format('m-d-Y');

echo 'Start date: ' . $date_of_oFour . '<br />Days since the start date: ' .  $days_since_oFour . '<br />30 days before the start date: ' . $days_subtracted_from_oFour;
?>

 

Tested.

 

:D Thanks for the awesome quick reply. But I get:

 

Fatal error: Call to undefined method DateTime::diff() in /home/sever/public_html/mav/datetest.php on line 36

 

http://www.sev3rance.com/mav/datetest.php

 

Edit: Sorry line 36 is:

$interval = $oFour->diff($today);

You can still take advantage of unix timestamps to handle these types of problems.

 

 

// Current Unix timestampdefine('SECONDSPERDAY', 60*60*24);$nowTS = time();// Your old date$oldTS = mktime(0,0,0,1,1,2004);// Date of your old date - 30$oldTS30prior = date('m-d-Y', $oldTS - (30 * SECONDSPERDAY));$daysSince = ($nowTS - $oldTS) / SECONDSPERDAY;echo 'Today:' . date('m-d-Y', $nowTS);echo ' 2004 date: ' . date('m-d-Y', $oldTS);echo " -30 days: $oldTS30prior ";echo " Days from 1-1-2004 to now: $daysSince";

 

My host is hopeless and refuses to do his job and update PHP.

 

gizmola that works great. Could you explain the variables and the

 

define('SECONDSPERDAY', 60*60*24);

 

Part?

 

I think your doing it in how many seconds since 2004-01-01, what's the reasoning behind that? Is it possible to do it with days?

With your help, I have done it :D

 

 

define('SECONDSPERDAY', 60*60*24); // 86,400 seconds in a day$startDateTime = "2007-04-16 19:47:00";$startYear = substr($startDateTime, 0, 4); // Get join Year (2007)$startMonth = substr($startDateTime, 5, 2); // Get join Month (04)$startDay = substr($startDateTime, 8, 2); // Get join Day (16)		$oFour = mktime(0,0,0,1,1,2004); // Get 2004 time$now = time(); // Get the current time$exactDaysSinceFour = ($now - $oFour) / SECONDSPERDAY; // Convert 2004 time into exact days$daysSinceFour = round($exactDaysSinceFour); // Round exact days$thirtyDaysAgo = $daysSinceFour - 30; // Take 30 away from todays days since 2004 to find out what it was 30 days ago$joinTime = mktime(0,0,0,$startMonth,$startDay,$startYear); // Get time character joined corporation$exactJoinDay = ($joinTime - $oFour) / SECONDSPERDAY; // Convert join time into exact days$joinDay = round($exactJoinDay); // Round exact daysif ($joinDay < $thirtyDaysAgo) {echo "Access Granted";} else {echo "Access Denied";}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.