Asked  9 Months ago    Answers:  5   Viewed   40 times

I am trying to output number of days between today and the date I enter so I have a problem I encounter error: "Warning: date_diff() expects parameter 2 to be DateTimeInterface" So what's the problem ?



echo date_diff($date,$today);




Your problem lies in that when using date_diff you have to make sure that you're comparing objects that are actual date objects. Also the return type for date_diff is a DateInterval object. You're treating it as a string.

$today = new DateTime(); // $today is a DateTime object
$date = new DateTime("2016-09-16"); // $date is also a DateTime object!
$diff =  date_diff($date,$today); // compare two objects of the same type FTW!

echo $diff->days; // $diff is a DateInterval object, so echo it's 'days' property.

// output: 3 (as of this writing)

Further reading:

Wednesday, March 31, 2021
answered 9 Months ago

If you don't care about perfect accuracy:

return $interval->days / 365;

You could also do something like return $interval->y + $interval->m / 12 + $interval->d / 365.

Didn't even notice your weird decimal convention until I saw @2unco's comment. That would look like: return $interval->y . '.' . $interval->m.

Friday, May 28, 2021
answered 7 Months ago

You need to convert your dates and date/times to an MS Excel serialized datetime stamp.... MS Excel does not automagically convert strings to dates for you, nor does simply setting a style change the actual value (a string in your case) that's stored in the cell in any way.

PHPExcel provides several different methods in the PHPExcel_Shared_Date class that will let you do these conversions.

$dto = new DateTime($result[$rowCount][$value]);
$dateVal = PHPExcel_Shared_Date::PHPToExcel($dto);
    ->setFormatCode(" h:mm");
$objPHPExcel->getActiveSheet()->setCellValue($column.$cell, $dateVal);
Friday, May 28, 2021
answered 7 Months ago

The second usort overrides the output of the first usort.

What you need to do is merge the 2 sorting functions to order themselves by title and then date time.

Pseudocode would be :

function SortByTitleAndDateTime($a,$b)
    1) SortByTitle and return the value if it is non zero.
    2) If SortByTitle is zero (meaning the 2 values are the same in terms of title), 
        do SortByDateTime

In the end, you'd only need to call usort(array, SortByTitleAndDateTime).

Saturday, May 29, 2021
answered 7 Months ago

strtotime will convert your date string to a unix time stamp. (seconds since the unix epoch.

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;
Tuesday, June 8, 2021
answered 6 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :