Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/310566-date-calculation/
Share on other sites

<?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

Link to comment
https://forums.phpfreaks.com/topic/310566-date-calculation/#findComment-1576807
Share on other sites

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.