Jump to content

[SOLVED] How to calculate days from variable date?


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.

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.