Jump to content

Recommended Posts

Hi I'm trying to get this to work. 

I have known date in MySQL database entered as 2019-08-01 and I need to calculate from that date to current time. 

What I have is this code

$date_ts = strtotime($row['date_ts']); //Timestamp
$date_str = date("M-d-Y", $date_ts); //String in format MMM-DD-YYYY
$TotalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days

but my result showing as ==> 18367 days

Could someone please help me get this to work so it shows the right number of days. thank you

 

Link to comment
https://forums.phpfreaks.com/topic/310592-php-difference-between-dates/
Share on other sites

I agree with Barand and requinix -- either use the functions in MySQL or use a DateTime.

With that said, your code didn't work because you inexplicably added a line to turn a perfectly valid timestamp number back into a string, and then tried to subtract it from the timestamp number.  If you hadn't done that your code would have worked!

 

<?php

$row['date_ts'] = '2019-08-01';

$date_ts = strtotime($row['date_ts']); //Timestamp
$totalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days

echo $totalTime;

// Outputs 259 days

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.