Jump to content

Calculating amount of days between two dates without using date functions


liquidentropy

Recommended Posts

Can anyone help me with this i'm not sure how to do it.  Still learning PHP and I cant quite figure it out

 

Write a function that takes two arbitrary dates in the format 'YYYY-MM-DD' and calculates the amount of days between them without using any php date functions.

 

Thanks

try

<?php
//Start
$day1 = "2008-01-10";
//End
$day2 = "2008-01-15";
//Time in seconds
$time = date("U",strtotime($day2))-date("U",strtotime($day1));
//86400 seconds in a day so
$days = round($time/86400);
echo "There are ".$time." seconds between ".$day2." and ".$day1." <br />and ".$days." days between ".$day2." and ".$day1.".";
?>

 

give that a go

 

Edit:

missed a ) so if you copied it once recopy it

But that uses the date function, cooldude82.

 

and I quote php.net

 

Requirements

 

No external libraries are needed to build this extension.

Installation

 

There is no installation needed to use these functions; they are part of the PHP core.

 

If its deafult installed you can't say I don't want to use it not a valid argument

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.