28, May 2012

Number of days between 2 mysql dates - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Number of days between 2 mysql dates
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Number of days between 2 mysql dates  (Read 1742 times)
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1525
6359 credits
Members referred : 8


Gimme all your cookies!!!


« on: Nov 02, 2005, 03:44:29 am »

Again I am always trying to improve my PHP scripts... Can anyone improve on this script or does anyone have a better one?

Code:
<?php

// returns the days between any 2 dates, any order
// date format required is mysql date format
// if date2 is null, then number of days between today and date1
function daysBtwDates ($date1$date2 null)
{
if ($date2 == null)
{
$date2 date("Y-m-d");
}

if ($date1 >= $date2)
{
$date3array explode("-"$date1);
$date4array explode("-"$date2);
}
else 
{
$date3array explode("-"$date2);
$date4array explode("-"$date1);
}

return (int)((mktime(0,0,0,$date3array[1],$date3array[2],$date3array[0]) - mktime(0,0,0,$date4array[1],$date4array[2],$date4array[0]))/86400);
}
?>


Thanks wineo


Last blog : Canonical URL Links / Tags
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Nov 02, 2005, 11:22:40 am »

I think your code is ok.

Something you could change is the first line. Instead of

Code:
<?php
if ($date2 == null)
{
            
$date2 date("Y-m-d");
}
?>

you can say :


Code:
<?php
if ($date2 == null)
{
            return 
0;
}
?>
« Last Edit: Nov 02, 2005, 11:24:14 am by Nikolas »

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
The mushroom boy
Hunky Junky Monky Man!
**
Posts: 65
418 credits
Members referred : 0



« Reply #2 on: Nov 02, 2005, 11:42:52 am »

Wineo, may I ask you why you have to know the number of days between two dates? (just courious)
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Nov 02, 2005, 11:45:58 am »

Wineo, may I ask you why you have to know the number of days between two dates? (just courious)

He. That's an interesting question Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1525
6359 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #4 on: Nov 02, 2005, 02:06:04 pm »

HAHA... I am making a graph using gd libraries and I don't want the entries in the x axis to just be evenly spaced but instead proportionate to the number of day bewteen each entry.

Nikolas - I just added that bit of code so that i (or whoever needs it) can also work out the number of days between today and any date that I enter, whether in the past or future. I only have to pass in one date if I need to calculate this.


Last blog : Canonical URL Links / Tags
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=584
Tags : php mysql Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Number of days between 2 mysql dates
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 06:06:37 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.