我正在尝试用php做一个时间跟踪系统,其中我有两个按钮,像时钟输入和时钟输出。我将时钟输入时间和时钟输出时间存储在会话变量中。现在我的要求是计算打卡时间和打卡时间之间的差值。如果你对计算时间之间的差异有一个清晰的想法,请与我分享你的想法
<?php
if(isset($_POST['start']))
{
date_default_timezone_set('Asia/Calcutta');
$time_start=date("Y-m-d h:i:s A");
setcookie('start',$time_start,time()+(36400)*3,'/');
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");
setcookie('end',$time_end,time()+(36400)*3,'/');
echo "time was ended".$time_end."<br>";
?>
<html>
<body>
<form method="POST">
<input type="submit" name="start" value="Start">
<br><br>
<input type="submit" name="end" value="End">
</form>
</body>
</html>发布于 2016-11-23 16:11:53
DateTime类有多种方法,可以使处理日期变得非常简单。也许下面的内容会让你对如何实现你的目标有所了解。
$format='Y-m-d H:i:s';
$timezone=new DateTimeZone('Asia/Calcutta');
$clockin = new DateTime( date( $format, strtotime( $_COOKIE['start'] ) ),$timezone );
$clockout= new DateTime( date( $format, strtotime( $_COOKIE['end'] ) ), $timezone );
$diff=$clockout->diff( $clockin );
echo $diff->format('%h hours %i minutes %s seconds');Further reading can be found here
为了全面测试,我很快就写了这篇文章--它看起来工作得很好!
<?php
$cs='shift_start';
$cf='shift_end';
date_default_timezone_set('Europe/London');
if( isset( $_POST['start'] ) ){
setcookie( $cs, date("Y-m-d h:i:s A"), time()+(36400)*3,'/');
}
if( isset( $_POST['end'] ) ){
setcookie( $cf, date("Y-m-d h:i:s A"), time()+(36400)*3,'/');
}
?>
<!doctype html>
<html>
<head>
<title>Set cookies and get time difference</title>
</head>
<body>
<form method="post">
<input type="submit" name="start" value="Clock-In">
<input type="submit" name="end" value="Clock-Out">
<br /><br />
<input type='submit' name='show' value='Show duration' />
</form>
<?php
if( isset( $_POST['show'] ) ){
$format='Y-m-d H:i:s';
$timezone=new DateTimeZone('Europe/London');
$clockin = new DateTime( date( $format, strtotime( $_COOKIE[ $cs ] ) ),$timezone );
$clockout= new DateTime( date( $format, strtotime( $_COOKIE[ $cf ] ) ), $timezone );
$diff=$clockout->diff( $clockin );
echo $diff->format('%h hours %i minutes %s seconds');
}
?>
</body>
</html>https://stackoverflow.com/questions/40758838
复制相似问题