0

hi i have the following error in my sent message file

Warning: date_diff() expects at least 2 parameters, 1 given in /home/letsswin/public_html/sent_mail.php on line 146
Show

Heres code section for error

<?php 
            $lastlog    = mysql_fetch_array(mysql_query(" select * from user_lastlogin  where online_status = '1' and user_id = '".$user_id."' "));
            $start_date = $lastlog['lastlogin']; 

            $online_status = $lastlog['online_status']; 
            ?>
            <?php if($online_status == '1') { echo date_diff($start_date); } ?>

I have previously add a similar error to this on a differrent file and tried adding that solution and it didnt work which was

$then = new DateTime($lastlogin['lastlogin']);
$now = new DateTime("now");
echo $then->diff($now)->format('%a days');

any ideas please would be much appreicated

2
Contributors
2
Replies
18
Views
2 Years
Discussion Span
Last Post by janicemurby
1

The date_diff() function requires two arguments, also both must be resources of the DateTimeInterface, if you pass a string, as in your example, the function will fail.

So, you must write:

<?php

    $date_a = '2015-04-01';
    $date_b = '2015-03-25';

    $dt_a = new Datetime($date_a);
    $dt_b = new Datetime($date_b);

    $interval = date_diff($dt_a, $dt_b);
    echo $interval->format('%R%a days'); # -7 days

Or use the object style, like your previous example. Note that the result is an object that cannot be converted directly to a string, so you must use the format() method.

Edited by cereal

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.