Re: counting with dates (help!)

From: Date: Mon, 07 Jan 2002 22:56:41 +0000
Subject: Re: counting with dates (help!)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-79735@lists.php.net to get a copy of this message
Well, I'll chime in as well. I'd recommend doing all your calculations in timestamps in seconds, then convert the results into days, dates, or whatever. If you only have a date to start with then convert to a timestamp, do the calculation, and convert back. You could wrap it in a function like this: <?php function dateDiffInDays($date1,$date2) {
    $date1Tm = strtotime($date1);
    $date2Tm = strtotime($date2);
    $diff = abs($date1Tm - $date2Tm) / 86400;
    return $diff;
} $today = date("Ymd",mktime(0,0,0, date("m"), date("d"), date("Y"))); $last_week = date("Ymd", mktime(0,0,0, date("m"), date("d")-7, date("Y"))); print("today: $today <br>\n"); print("last_week: $last_week <br>\n"); print("diff in days: " . dateDiffInDays($last_week, $today) . "<br>\n"); ?> On Thursday, February 7, 2002, at 04:21 PM, Sander Peters wrote:
Hello, This is my problem: $today = date("Ymd", mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date("Ymd", mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! How can I let php count in real days/month/years in stead of numbers? Maybe this is a silly question, but anyone who has the answer would help me very much! Thanks in advance! -- Met vriendelijke groet / With Greetings, Sander Peters site: http://www.visionnet.nl/ email: mailto:s.peters@visionnet.nl webmail: mailto:sanderpeters@yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net


« previous php.general (#79735) next »