Laravel 5.1 - 检查访问者是否已访问过个人资料(unix时间戳)

时间:2015-12-28 13:22:59

标签: php laravel

我有一个包含以下列的表。

  
      
  • id (个人资料ID) int(10)
  •   
  • viewer (访客ID) int(10)
  •   
  • ip (访问者IP) int(10)
  •   
  • ts (unix timestamp) int(10)
  •   

现在,我想询问访客x是否今天访问过个人资料。

我的想法是,我想做这样的事情。

public function hasVisitToday(Profile $profile)
{
    $dt = Carbon::now();

    if(ViewTrack::where('id', $profile)->where('viewer', $visitor)->whereDate('ts', $dt->today())->first())
        return true;

    return false;
}

该项目使用Laravel 5.1,其中一个主要问题是数据库将时间戳存储为int列中的“unix timestamp”而不是timestamp列。

我正在寻找处理此问题的最佳方法。

2 个答案:

答案 0 :(得分:0)

if(ViewTrack::where('id', $profile)
    ->where('viewer', $visitor)
    ->where('ts', '>=', $dt->startOfDay()->timestamp())
    ->count())

如果它们的格式相同,您应该可以比较Unix时间戳。

我倾向于同意上述评论,我宁愿以人类可读的格式提供它,以便我可以看到我是否有任何奇怪的日期。

如果您有任何有趣的事情,请确保您的服务器,MySQL服务器和laravel配置时区都匹配。

答案 1 :(得分:0)

感谢Zoe, 但我选择whereBetween语句,这是我的解决方案。

        $carbon = Carbon::now();
        $start  = Carbon::now()->startOfDay();

        $visit = ViewTrack::where('id', $this->id)
            ->where('viewer', $visitor->id)
            ->whereBetween('ts', [$start->timestamp , $carbon->timestamp])
            ->get();

        if($visit->count() > 0)
            return true;

        return false;
相关问题