I need to extract only the day number of a user’s registration date.
And extract only the day number of the current date.
Simply in an if loop, say if the day number the user registered is equal to the day number of the current date, do this, or do that.
Code:
$manager = "Manager"; $managerPRO = "ManagerPRO"; $q = $connessione->prepare(" SELECT * FROM collaboratori WHERE cat_professionisti = ? OR cat_professionisti = ? "); $q->bind_param('ss', $manager,$managerPRO); $q->execute(); $r = $q->get_result(); while($rr = mysqli_fetch_assoc($r)){ /*REGISTRATION DATE*/ $registrazione = $rr['data_registrazione']; $timestamp = strtotime($registrazione); echo date("d", $timestamp) .'=' ; /*CURRENT DATE*/ $data_corrente = date('Y-m-d'); $timestamp_uno = strtotime($data_corrente); echo date("d", $timestamp_uno); /*CONTROL*/ if ($timestamp == $timestamp_uno){ echo "yes".'<br>'; }else{ echo "no".'<br>'; } }
Result:
18=18no 17=18no 16=18no 16=18no
Why in the first case if 18 = 18 gives me false?
However, if I change the date of the user’s registration and therefore the first 18, from 2020/11/18 to 2020/12/18, then the current month gives me yes!
I need that regardless of the month, just by checking the day if it is the same, tell me yes, where am I wrong?
Advertisement
Answer
You are comparing timestamps, which are measured in seconds. What you are doing is effectively comparing two different points in time, not the days of the month.
You really should be using DateTime
. If you want to compare only the day part then you can do something like this.
$dt1 = new DateTime($registrazione); $dt2 = new DateTime(); // defaults to now if($dt1->format('d') === $dt2->format('d')) { echo "Yes, it's the same day of the month"; } else { echo 'no!'; }