sfia Posted April 14, 2020 Share Posted April 14, 2020 Hi, Im trying to calculate days between two dates and add that to the php code below. I have this code that pulls records from my database and outputs it as json. <?php $servername = "localhost"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; $connect = mysqli_connect("localhost", "xxxxx", "xxxxx","xxxxx"); $sql = "SELECT * FROM solstice"; $result = mysqli_query($connect, $sql); $json_array = array(); while($row = mysqli_fetch_assoc($result)) { $json_array[] = $row; } /*echo '<pre>'; print_r('records:'); echo '</pre>';*/ echo json_encode($json_array); ?> Basically I was thinking something like this but I don't know how I can add this to php above that produces json . This information is calculated on the fly. I need to add TotalTime and Date3 $Date1_ts = strtotime($row['Date1']); //Timestamp $Date1_str = date("M-d-Y", $Date1_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $Date1_ts)/(60*60*24)) . ' days';//Total in days $start_ts = strtotime($row['Date1']); $end_ts = strtotime($row['Date2']); $date_diff = ($end_ts - $start_ts); $Date3= ($date_diff>0) ? floor($date_diff/ (60*60*24)) : ''; // days difference from start to finish I would be really grateful if someone could help me with this. Thank you. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 14, 2020 Share Posted April 14, 2020 First please use the code icon (<>) and select PHP for your code. It makes it much easier to read. Once you convert dates to 'strtotime' date calculations are easy. You can simply subtract the 2 dates and convert the result back to a date format. Quote Link to comment Share on other sites More sharing options...
chhorn Posted April 14, 2020 Share Posted April 14, 2020 just use the DateTime class with the diff method. Quote Link to comment Share on other sites More sharing options...
sfia Posted April 14, 2020 Author Share Posted April 14, 2020 <?php $servername = "localhost"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; $connect = mysqli_connect("localhost", "xxxxx", "xxxxx","xxxxx"); $sql = "SELECT * FROM solstice"; $result = mysqli_query($connect, $sql); $json_array = array(); while($row = mysqli_fetch_assoc($result)) { $json_array[] = $row; } /*echo '<pre>'; print_r('records:'); echo '</pre>';*/ echo json_encode($json_array); ?> Sorry, I don't know if i explained it right. But I would like to add calculation of time to that PHP code that I have. I think I'm doing the calculation correct already, but I'm stuck as how to have it show up along with my other json result from PHP. $Date1_ts = strtotime($row['Date1']); //Timestamp $Date1_str = date("M-d-Y", $Date1_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $Date1_ts)/(60*60*24)) . ' days';//Total in days $start_ts = strtotime($row['Date1']); $end_ts = strtotime($row['Date2']); $date_diff = ($end_ts - $start_ts); $Date3= ($date_diff>0) ? floor($date_diff/ (60*60*24)) : ''; // days difference from start to finish Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2020 Share Posted April 14, 2020 or do it in the query when you get the data (and you can reformat the dates too) SELECT date_format(Date1, '%b-%d-%Y') as dstr1 , date_format(Date2, '%b-%d-%Y') as dstr2 , datediff(Date2, Date1) as days ... Quote Link to comment Share on other sites More sharing options...
sfia Posted April 14, 2020 Author Share Posted April 14, 2020 thanks so much guys. I will give it a try. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.