日期时间插入表

时间:2016-06-30 15:03:26

标签: php datetime

我将日期/时间保存在我的sql表中作为一列" registertime"。

$stmt = $mysqli->prepare("SELECT username, firstname, lastname, registerdate FROM users WHERE level < 1");
$stmt->bind_param('ssss', $_GET['username'], $_GET['firstname'], $_GET['lastname'], $_GET['registerdate']);
$stmt->execute();
$stmt->bind_result($username, $firstname, $lastname, $registerdate);

我想使用我的注册日期值来确定有多少天前有人注册使用(日期(&#39; Y-m-d H:i:s&#39;)

<?php
$today = strtotime(date('Y-m-d H:i:s'));
$dayspassed = strtotime($registerdate));
$daysinactive = $dayspassed - $today;
?>

我有用户注册存储在$ registerdate中的日期我想知道自$ registerdate和当前日期过去了多少天,所以我可以回应&#34;这个用户在2天前注册了&#34;

2 个答案:

答案 0 :(得分:1)

您可以使用日期时间类

<?php
$bday = new DateTime('2016-05-016'); //y-m-d
$today = new DateTime('2010-08-01 00:00:00'); // for testing purposes

$diff = $today->diff($bday);

printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);

答案 1 :(得分:1)

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');

输出:+ 2Days