Jump to content

[SOLVED] How to calculate days from variable date?


benphp

Recommended Posts

This will be easy for one of you gurus. I want to fetch the date from a variable date, for example:

 

<?php
$mydate = "";
$daysaway = 400;
$startdate = "5/5/2005";

//I need how to get the date that was 400 days ago

print "$mydate was $daysaway days from $startdate.";
?>

 

 

 

Assuming your start date is mm/dd/YYYY -

<?php
$daysaway = 400;
$startdate = "5/5/2005";

//I need how to get the date that was 400 days ago
$mydate = date('m/d/Y',strtotime("$startdate - $daysaway days"));

print "$mydate was $daysaway days from $startdate.";
?>

Your question is beyond unclear.  "I want to fetch the date from a variable date" could be interpreted a bunch of different ways.  Your example code doesn't help.  What are you attempting to derive?

 

$startdate - $daysaway = Answer is a Date.

 

or

 

$mydate = $startdate?  = Answer is number of days difference

 

 

 

 

 

No it wasn't clear at all.  PFMaBiSmAd just made an assumption, and guessed right.  Sometimes that happens, so you got lucky, nothing more.  In the future try and work a bit harder when you're formulating your questions.

I use mysql to get a lot of my dates.

 

example:


<?php
require ('dbcon.php');
    
$startdate = date('Y-m-d');
$days = 400;
$query = "SELECT DATE_SUB( '$startdate', INTERVAL $days DAY )";
$newdate = $dbLayer->FetchRow($query);
echo $newdate;
           
?>

 

For adding:

<?php
require ('dbcon.php');
    
$startdate = date('Y-m-d');
$days = 400;
$query = "SELECT DATE_ADD( '$startdate', INTERVAL $days DAY )";
$newdate = $dbLayer->FetchRow($query);
echo $newdate;
           
?>

 

I use a custom mysqli class from http://www.robpoyntz.com/blog/?p=189 for the db connection.

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.