Jump to content

Trying to add days to a date, failing miserably..


Go to solution Solved by Psycho,

Recommended Posts

i have a system that deletes files after a certain time so that the dir wont get flooded with files... 
but anyway, im trying to add the lifespan of a file based on its size to the current date, so that the server knows exactly wenn to delete those files...
 
so lets say the file need's to be deletet in 21 days 
 
i try'd to handle it this way...
$del_tim_holder_0 = 21;
$del_tim = date("d.m.Y", date("d.m.Y") + ($del_tim_holder_0 * 7 * 86400));

but it just shows 01.01.1970 

i want to mention that in the original code it is an string that' been converted to an array for the actual amount of days to choose from... and as for the amount of files it chooses what fits in the most practical way...

 

#for example:
$del_tim_holder_0 = "15:18:21";
$del_tim_holder_1 = split(":", $del_tim_holder_0);
$del_tim .= date("d.m.Y", date("d.m.Y") + ($del_tim_holder_1[count($del_tim_holder_1)] * 7 * 86400));

any suggestions ?

Edited by AzeS

$date = '16.02.2017';
$days = 21;

// create dateTime object
$dt = new DateTime($date);

// create a dateInterval
$di = new DateInterval("P{$days}D");

// add interval to the date
echo $dt->add($di)->format('d.m.Y'); //--> 09.03.2017

Still not working  : 
 

$this->date = new DateTime(date("d.m.Y"));

private function set_interval($data) {
	$interval = new DateInterval("P{$data}D");
	return $interval;
}

echo $this->date->add($this->set_interval($del_tim_holder_1[count($del_tim_holder_1)] * 7)->format("d.m.Y"))


Your final ")" is in the wrong place. It needs to be in the position marked below

echo $this->date->add($this->set_interval($del_tim_holder_1[count($del_tim_holder_1)] * 7))->format("d.m.Y");
                                                                                          ^

  • Solution

I don't think this will work:

$del_tim_holder_1[count($del_tim_holder_1)]

Assuming a zero based index, an array with a count of '3' would have indexes of '0', '1', & '2'. There would be no value with an index of '3'. You could instead use array_pop or, instead of having to use split/explode first to create an array, just use string functions (see example below).

 

Also, rather than only put in the logic for the interval in the function, I would suggest just passing the $del_tim_holder_0 value and have the function do all the logic to return the applicable date string.

private function calculateDate($intervalStr)
{
    //Get value following last ':' and ensure a number
    $inervalWeeks = intval(substr(strrchr($intervalStr, ":"), 1));
    //Convert number to weeks
    $intervalDays = $inervalWeeks * 7;
    //Create a date intervale
    $interval     = new DateInterval("P{$intervalDays}D");
    //Create current date object
    $currDate     = new DateTime(date("d.m.Y"));
    //Apply the interval and convert to string
    $newDate      = $currDate->add($interval)->format('d.m.Y');
    return $newDate;
}
 
$del_tim_holder_0 = "15:18:21";
$this->date = $this->calculateDate($del_tim_holder_0);
// $this->date = '13.07.2017'
Edited by Psycho
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.