0

im trying to get the date diff:

<?php
$datetime1 = date_create(date('d - M - Y'));
$datetime2 = date_create($date);
$interval = date_diff($datetime1, $datetime2);
$diff = $interval->format('LISTED %R%a DAY(S) AGO');
?>

it works in my localhost host.. but when I upload and run it online it get this error :

Fatal error: Call to undefined method DateTime::diff()

please help; =(
thnx

5
Contributors
9
Replies
43
Views
3 Years
Discussion Span
Last Post by kaleemullah360
0

so it means my web host is not yet updated? =(

Edited by markii.borabon_1: grammar

0

its there any other way to fix my problem sir? any idea.. thnx =)

Edited by markii.borabon_1: grammar

0

Make sure which version of PHP is in use in your host, you can use phpinfo(): http://php.net/manual/en/function.phpinfo.php and if you can upgrade.

For an alternative solution check the comments in the linked documentation, there are some examples with DateTime and strtotime().

In addition you can execute the same operation in MySQL, examples:

> select datediff(now(), date_sub(now(), interval 3 month)) days;
+------+
| days |
+------+
|   92 |
+------+
1 row in set (0.00 sec)

> select datediff(now(), '2014-08-15') days;
+------+
| days |
+------+
|   42 |
+------+
1 row in set (0.00 sec)

So it depends from where this information comes from (database, users forms) and what you want to achieve.

0

Try this it's working perfectly.

<?php
    $start_date='some date';
    $end_date='some date';
    $interval = new DateInterval("P1D");
    $begin = new DateTime($start_date);        
    $end = new DateTime( $end_date);
    $period = new DatePeriod($begin, $interval,$end);
    print_r($period);
?>
0

It will help u.It Working below php5.3

<?php
        function dateDifference($startDate, $endDate)
        {
            $startDate = strtotime($startDate);
            $endDate = strtotime($endDate);
            if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate)
                return false;

            $years = date('Y', $endDate) - date('Y', $startDate);

            $endMonth = date('m', $endDate);
            $startMonth = date('m', $startDate);

            // Calculate months
            $months = $endMonth - $startMonth;
            if ($months <= 0)  {
                $months += 12;
                $years--;
            }
            if ($years < 0)
                return false;

            // Calculate the days
                        $offsets = array();
                        if ($years > 0)
                            $offsets[] = $years . (($years == 1) ? ' year' : ' years');
                        if ($months > 0)
                            $offsets[] = $months . (($months == 1) ? ' month' : ' months');
                        $offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) : 'now';

                        $days = $endDate - strtotime($offsets, $startDate);
                        $days = date('z', $days);   

            return array($years, $months, $days);
        }
?>
0

Hi just a stupid question do you have a default timezone included or activated? if no include in it at the beginning your your code or activate it on your server.

date_default_timezone_set('Asia/Manila');
$date="10-10-2014";
$datetime1 = date_create(date('d - M - Y'));
$datetime2 = date_create($date);
$interval = date_diff($datetime1, $datetime2);
echo $diff = $interval->format('LISTED %R%a DAY(S) AGO');
0

The easiest and good method to claculate exact date time is TIMESTAMP.
claculate time stamp of first date and second date then subtrac them that will be your required date in the form of time TIMESTAMP cgange it to human read able form using TIMESTAMP functions

<?php 
//Our dates
$date1 = "2013-03-01 19:12:45";
$date2 = "2014-03-01 06:37:04";

//Convert them to timestamps.
$date1Timestamp = strtotime($date1);
$date2Timestamp = strtotime($date2);

//Calculate the difference.
$difference = $date2Timestamp - $date1Timestamp;

echo $difference;
?>

For Reference

This topic has been dead for over six months. 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.