0

I am trying to get the create date and finding the date diff but was not able to do it. I was able to use new DateTime to use date1.diff as I am getting the date from file with filemtime.

Can someone point me to right direction? Thanks.

<?php
$_filename = realpath('test.txt');
if (file_exists($_filename)) {
    $crdatefile = date("Y-m-d H:i:s",filemtime($_filename));
    $date1 = date("Y-m-d H:i:s");
    $datediff = $crdatefile - $date1;
    echo '$datediff' . $datediff . '____';
    if ($datediff < 0) {$datediff = $datediff * -1;}
echo '$datediff' . $datediff . '____';

    $days = round($datediff / (60 * 60 * 24));

echo 'days' . $days . '____';
    if ($days) {
        echo 'days ' . $days . '____';
        if ($days > 1) {
        //unlink($_filename);
            echo 'file delete' . '____';
        }
    }
    echo 'file after delete' . '____';
}
?>
Onur Topal
  • 3,042
  • 1
  • 24
  • 41
  • What **exactly** is not working with the code you are using? How is that "date diff" defined? – Nico Haase Sep 23 '22 at 08:38
  • you get : Notice: A non-well-formed numeric value encountered in ... right? – Mahmut Salman Sep 23 '22 at 08:41
  • you can also read [this](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Mahmut Salman Sep 23 '22 at 08:45
  • @NicoHaase the current problem is $datediff is always zero. when I tried to use new DateTime I couldn't convert the int value (filemtime) to correct DateTime, – Onur Topal Sep 23 '22 at 09:00
  • "$datediff is always zero" - what have you tried to resolve the problem? What makes you think that you can subtract two strings? – Nico Haase Sep 23 '22 at 09:09
  • @NicoHaase based on this answer https://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates also from this answer i made the code https://stackoverflow.com/questions/20617100/php-converting-integer-to-date-reverse-of-strtotime. – Onur Topal Sep 23 '22 at 09:20

1 Answers1

1

You can do this pretty easliy, but I'd use filectime instead, since on Windows this would give you the actual creation date, whereas on a Linux systeme, it gives you the date of the last change, which is the best you can get, since no creation dates exist on Linux.

// create a new DateTime object with the timestamp returned by the file function
// pass it as format "U" (unix timestamp)
$fileTime = DateTime::createFromFormat('U', filectime($_filename));

// now just diff from a fresh DateTime object (date = now)
$daysSinceFileTime = $filetime->diff(new Datetime())->days;
stui
  • 353
  • 3
  • 15
  • Note: Tools like Explorer show the date in local time. With `$fileTime = DateTime::createFromFormat('U', filectime($_filename));` however, an object is generated with UTC time zone and UTC time. This time can deviate from local time by several hours. The difference in days to `new Datetime()` is then correct again, since DateTime processes the time zones correctly when calculating. – jspit Sep 29 '22 at 06:35