日期转 int / float
select CAST(GETDATE() AS FLOAT)
等同于:
$baseDate = strtotime('1900-01-01'); // 获取"1900-01-01"的时间戳
$now = time(); // 获取当前时间戳
$diff = $now - $baseDate; // 计算当前时间与"1900-01-01"之间的时间差(以秒为单位)
echo (float) $diff / 86400;
select CAST(CAST(GETDATE() AS FLOAT) AS INT)
等同于:
1、
$baseDate = Carbon::create(1900, 1, 1); // 创建 Carbon 对象,表示"1900-01-01"
$now = Carbon::now()->addDay(); // 获取当前时间对应的 Carbon 对象
$diffInDays = $now->diffInDays($baseDate); // 计算当前时间与"1900-01-01"之间的天数差
echo $diffInDays.PHP_EOL; // 输出日期的天数差
2、
$baseDate = strtotime('1900-01-01'); // 获取"1900-01-01"的时间戳
$now = time(); // 获取当前时间戳
$diff = $now - $baseDate; // 计算当前时间与"1900-01-01"之间的时间差(以秒为单位)
echo intval ($diff / 86400) .PHP_EOL;
3、
$baseDate = strtotime('1900-01-01'); // 获取"1900-01-01"的时间戳
$now = time(); // 获取当前时间戳
$diff = $now - $baseDate; // 计算当前时间与"1900-01-01"之间的时间差(以秒为单位)
$dateString = date('Y-m-d', $now); // 将当前时间戳格式化为"Y-m-d"格式的日期字符串
$dateTimestamp = strtotime($dateString); // 将日期字符串转换为时间戳
$intValue = intval(($dateTimestamp - $baseDate) / 86400); // 计算当前日期与"1900-01-01"之间的天数差,并将其转换为整数
int / float 转日期
方法1:SELECT CAST(45066.567326389 AS DateTime);
方法2:SELECT CONVERT(INT, CONVERT(DATETIME, '2023-09-01')); // 指定日期