Parsing String to Timestamp in order to find difference in days

Question

I need to find the difference in days between two timestamps.

$now = date("Y-m-d");
//This return 2017-01-04

$last_active = date("Y-m-d", strtotime($user->updated_at->toDateTimeString())); 
//This returns for example: 2016-07-20

$datediff = $now - $last_active;
$days = floor($datediff/(60*60*24));

This code keeps returning 0 for $days.

However it seems that the $now and $last_active works as strings, and thereby i cant just substract the two from eachother, and then $days will simply return 0.

How do I substract the two from eachother? I need someway to parse them into some sort of variable, where I can substract the two.


So far ive tried the following:

Finding the number of days between two dates This does not work, because my variables are as strings, is my guess.

Also I have tried parsing the dates into integers, but this would result in, for example 20170104 - 20160720 which gives a value that has nothing to do with the difference in days from the two.


Hoping for some helpful pointers

Best Regards, Patrick


Show source
| laravel   | date   | php   | string   | parsing   2017-01-04 18:01 4 Answers

Answers to Parsing String to Timestamp in order to find difference in days ( 4 )

  1. 2017-01-04 18:01

    Since you're using Laravel, you can use Carbon out of the box. Use diff...() methods including diffInDays, diffForHumans() and others:

    Carbon::parse($stringDate)->diffInDays(Carbon::now());
    

    If you need compare two days (not difference from now), just use another parse() instead of now().

  2. 2017-01-04 18:01

    Difference in days using the PHP diff() function

    $now = new DateTime();
    $last_active = new DateTime($user->updated_at->toDateTimeString());
    $interval = $now ->diff($last_active);
    $difference = $interval->format('%a');
    echo $difference;
    
  3. 2017-01-04 18:01

    You can do this very easily using Carbon. http://carbon.nesbot.com/docs/#api-difference

    If you are working with strings, you should be able to use

    $date1 = Carbon\Carbon::parse('January 4th 2017');
    $date2 = Carbon\Carbon::parse('January 1st 2017');
    

    That will give you two Carbon objects for which you can now use to compare.

    $days = $date1->diffInDays($date2);
    

    With Laravel timestamps, you get a carbon object out of the box. So using your example you should be able to do this.

    $days = $user->updated_at->diffInDays(Carbon::now());
    
  4. 2017-01-04 18:01

    Use strtotime() on the two dates for comparison, to calculate them both as the number of seconds since the UNIX epoch ( January 1 1970 00:00:00 UTC).

    http://php.net/manual/en/function.strtotime.php

    Example:

    $now = date("Y-m-d");
    $last_active = date("Y-m-d", strtotime($user->updated_at->toDateTimeString())); 
    $current = strtotime($now);
    $last = strtotime($last_active);
    $diff = $current - $last;
    $days = floor($diff/(60*60*24));
    

Leave a reply to - Parsing String to Timestamp in order to find difference in days

◀ Go back