5, December 2008

Number of days between 2 mysql dates - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ 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

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


Gimme all your cookies!!!


« on: Nov 02, 2005, 04: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 : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8272
42619 credits
Members referred : 3



« Reply #1 on: Nov 02, 2005, 12:22:40 PM »

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, 12:24:14 PM by Nikolas »

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

Last blog : Free Unlimited Bandwith and disk space to good to be true?
The mushroom boy
Hunky Junky Monky Man!
**
Posts: 65
418 credits
Members referred : 0



« Reply #2 on: Nov 02, 2005, 12:42:52 PM »

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: 8272
42619 credits
Members referred : 3



« Reply #3 on: Nov 02, 2005, 12:45:58 PM »

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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #4 on: Nov 02, 2005, 03: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 : Site of the Month - August 2007
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....

Topic sponsors:
Get a permanent link here for $1.99!


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?
Dec 05, 2008, 01:06:26 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!


Forum Statistics
Total Posts: 38.000
Total Topics: 7.685
Total Members: 4.470
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: srinivasarao

28 Guests, 4 Users online :

9 users online today:



Readers

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