How to store session in when click button start to again click button end time for calculate start to end time in php

无人久伴 提交于 2019-12-08 12:07:30

问题


I want to chart for daily spending hours in browser. In the chart each day spending hours are mentioned. When I click the button time will start and again click the button time will end and finally calculate start to end time stored in like session or any other. This will happen for three days and finally chart will generate.

HTML CODE:

<html>
    <body>
        <form action="time_calculation.php" method="POST">
            <input type="submit" name="start" value="Start">
            <br><br>
            <input type="submit" name="end" value="End">
        </form>
    </body>
</html>

php:

<?php
session_start();
if(isset($_POST['start'])) {
    date_default_timezone_set('Asia/Calcutta'); 
    $time_start=date("Y-m-d h:i:s A"); 
    $_SESSION['start']='$time_start';
    echo "time start now ".$time_start."<br>";
}

if(isset($_POST['end'])) {
    date_default_timezone_set('Asia/Calcutta'); 
    $time_end=date("Y-m-d h:i:s A"); 
    $_SESSION['end']='$time_end';
    echo "time was ended".$time_end."<br>"; 
    $sst=strtotime($_SESSION['start']);
    $eet=strtotime($_SESSION['end']);
    $diff=$eet-$sst;
    $timeelapsed=mktime("h:i",$diff);
    echo $timeelapsed;
}
?>

回答1:


You can store the active session times on the database temporary table and calculate it for 3 days.

I am not clear with your requirements, if you need to track the details you can use available tools like Google analytics




回答2:


mktime() is used to create a timestamp from an array of inputs. PHP Man

I would suggest you change the code to take your date from the session and turn it back into a DateTime object. Then you can use DateInterval to get the difference in the DateTime and format the output to your liking.

$sst= new DateTime($_SESSION['start']);
$eet= new DateTime($_SESSION['end']);
$diff=date_diff($eet, $sst);
$timeelapsed=$diff->format('%r %a days %h hours and %i minutes');
echo $timeelapsed;

edit: Your php also has an issue with quoting your variables. Eg. You should not quote $time_start when trying to assign it's value to $_SESSION['start']. It is trying to assign the literal string, rather than the date object assigned to the variable.

edit 2: Here is the full code (changed to single page form/php)

<?php
session_start();

?>

<html>
    <body>
        <form action="thisForm.php" method="POST">
            <input type="submit" name="start" value="Start">
            <br><br>
            <input type="submit" name="end" value="End">
        </form>
    </body>
</html>

<?php
if(isset($_POST['start'])) {
    date_default_timezone_set('Asia/Calcutta'); 
    $time_start = date("Y-m-d h:i:s A"); 
    $_SESSION['start'] = $time_start;
    echo "time start now $time_start <br>";
}

if(isset($_POST['end'])) {
    date_default_timezone_set('Asia/Calcutta'); 
    $time_end = date("Y-m-d h:i:s A"); 
    $_SESSION['end'] = $time_end;
    echo "time was ended $time_end <br>"; 
    $sst= new DateTime($_SESSION['start']);
    $eet= new DateTime($_SESSION['end']);
    $diff = date_diff($eet, $sst);
    $timeelapsed = $diff->format('%r %a days, %h hours, %i minutes and %S seconds');
    echo $timeelapsed;
}
?>


来源:https://stackoverflow.com/questions/40756643/how-to-store-session-in-when-click-button-start-to-again-click-button-end-time-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!