Jump to content

Calculating amount of days between two dates without using date functions


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

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.