AzeS Posted February 16, 2017 Share Posted February 16, 2017 (edited) 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 February 16, 2017 by AzeS Quote Link to comment Share on other sites More sharing options...
Barand Posted February 16, 2017 Share Posted February 16, 2017 $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 Quote Link to comment Share on other sites More sharing options...
AzeS Posted February 16, 2017 Author Share Posted February 16, 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")) Quote Link to comment Share on other sites More sharing options...
Barand Posted February 16, 2017 Share Posted February 16, 2017 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"); ^ Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted February 16, 2017 Solution Share Posted February 16, 2017 (edited) 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 February 16, 2017 by Psycho Quote Link to comment Share on other sites More sharing options...
AzeS Posted February 17, 2017 Author Share Posted February 17, 2017 I'ts solved now, thanks again ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.