phpunixpst

Displaying unix time in PST shows an extra hour


So I've a very basic form setup and I've this query running once it gets submitted

$insert = "INSERT INTO data(name, time, time_end) 
VALUES ('".$name."', '".time()."', '".time()."' + 176400) ;";
mysqli_query($conn, $insert);

and I've this mentioned in the header file to display time in PST

date_default_timezone_set('America/Los_Angeles');
$time = time();

and to display the all the info from 'data' in PST, I've this code in a diff page

$session = "SELECT * from data";
$session_query = mysqli_query($conn, $session); ?>

<table>

  <tr>
    <th>Name</th>

    <th>Started at</th>
    <th>Ending at</th>
  </tr>

<?php

while($data = mysqli_fetch_assoc($session_query)) { ?>

 <tr>
    <th><?php echo $data['name']; ?></th>



    <th><?php

    $start = $data['time'];
    echo date("F j, Y, g:i a",$start); ?></th>

    <th><?php
    $end = $data['time_end'];
    echo date("F j, Y, g:i a",$end); ?></th>


  </tr>

  <?php

}
?>

</table>
<?php } ?>

This seems to work fine only till I add below or 48 hours. As in if I add +172800 in the insert query, it does display

March 7, 3:30 am - March 9, 3:30 am

But whenever I try to add above 48hours(in seconds) in insert query, it keeps adding an extra hour, like when I add 72 hours

 March 7, 3: 30 am - March 10, 4:30 am

So I don't get why its displaying March 10, 4:30 am instead of 3:30 am (as in extra one hour), Can someone please tell how to fix it.


Solution

  • The easy solution is using strtotime:

    date_default_timezone_set('America/Los_Angeles');
    $insert = "INSERT INTO data(name, time, time_end) VALUES ('".$name."', '". strtotime('now')."', '". strtotime('+2 days')."') ;";
    mysqli_query($conn, $insert);
    

    You seem to have an sql injection vulnerability in your code. You should read this: How can I prevent SQL injection in PHP?