开发者

How to compare dates in PHP

开发者 https://www.devze.com 2023-03-30 06:30 出处:网络
I am setting up recent activities function where I get the all user activity from this week. However I don\'t know how to get all activity from this week as I need to compare the dates between now and

I am setting up recent activities function where I get the all user activity from this week. However I don't know how to get all activity from this week as I need to compare the dates between now and a week before now开发者_开发问答. Please can you tell me how I can do this? I am using a MySQL database to hold all of the information. I have the tables, likes, follows, comments and posts. I would obviously need to compare dates to get all of the information from all of the tables.

I use this method when I insert dates into my MySQL db.

function uk_date() {
        $sign = "-"; 
        $h = "0"; 
        $dst = "true"; 
        if ($dst) {
            $daylight_saving = date('I');
            if ($daylight_saving){
               if ($sign == "-"){ $h=$h-1;  }
               else { $h=$h+1; }
          }
        }
        $hm = $h * 60;
        $ms = $hm * 60;
        if ($sign == "-"){ $timestamp = time()-($ms); }
        else { $timestamp = time()+($ms); }
        return $gmdate = gmdate("d/m/Y g:i:s A", $timestamp);
    }

Also, the field "date" in my db is not a timestamp, it is a string generated from the uk_date() method above. This is where I am unsure of how to compare the dates.


select * from table where datetime_field > now() - interval 7 day


Don't exactly understand, why this need to be so complicated, why to work with daylight_saving etc...

Is it possible, that you simply store to database the result of call to time() ? Then you'll have unix-timestamp, and that is easily comparable with any other desired unix timestamp. All arithmetic is trivial then... am I right ?

0

精彩评论

暂无评论...
验证码 换一张
取 消