max_
max_

Reputation: 24481

How to compare dates in PHP

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.

Upvotes: 1

Views: 316

Answers (2)

Roman Pietrzak
Roman Pietrzak

Reputation: 615

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 ?

Upvotes: 1

Nicola Cossu
Nicola Cossu

Reputation: 56357

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

Upvotes: 1

Related Questions