Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

today's date

I want to capture today's date. How to do so?

$date = new DateTime();

    $today = date('yy-mm-dd');

    dd($today);  

I put this script in the controller. I wonder why it keep bypassing the dd($today)?

0 likes
3 replies
tykus's avatar

What you have is fine, but you don't need $date = new DateTime(), and the correct format string would be:

date('y-m-d') // 18-10-12
// or
date('Y-m-d'); // 2018-10-12

There is a Carbon instance returned from this method which you can format as you please:

today()
davy_yg's avatar
Level 27

Then I try to compare the date with date and time. As long as the date is the same without considering the time, then it should be counted.

$count_tgl = TrxHarianModel::select('paydate')
                 ->where('paydate', '=', $today)
                 ->count();

I wonder why the result is 0. I already tested by inputing the value of today:

     $today = '2018-08-02';
tykus's avatar

Equality is a tough test in a standard where, I suggest whereDate instead which will cast the datetime field in the database to a date before making the comparison:

$count_tgl = TrxHarianModel::select('paydate')
    ->whereDate('paydate', '=', $today)
    ->count();

Please or to participate in this conversation.