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

COUPDEGRACES's avatar

Date automate changed to 1970-01-01

I have table called "iklan", and i have column "waktupesan" format date example

2021-17-03

I want to display the data 2021-17-03 to my tabel, when im try to show data, The browser shows a different time than what I was looking for .

is my code for result.blade.php

 <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered" width="100%" cellspacing="0">
                        <thead>
                            <tr>
                                <th width="2%">#</th>
                                <th width="20%">Koran</th>
                                <th>Info Iklan</th>
                                <th width="20%">Diterima</th>
                                <th width="15%">Harga</th>
                            </tr>
                        </thead>
                        <tbody>
                        @php
                        $no=1;
                        @endphp
                        @foreach($orders as $order)
                        <tr>
                            <td>{{ $no++ }}</td>
                            <td>{{ $order->subdivisi }}</td>
                            <td>{{ $order->title }}</td>
                            <td>{{ $order->waktupesan }}</td>
                            <td>{{ $order->harga }}</td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                    <h5>Total: {{ $sum }}</h5>
                </div>
            </div>

ReportController

 public function checkReport(Request $request){
        if ($request->date){
            $date = date('Y-d-m ', strtotime($request->date));
            $orders = DB::table('iklan')->where('waktupesan',$date)->get();
            $sum = DB::table('iklan')->where('waktupesan',$date)->sum('harga');
            return view('laporan.result',compact('orders','sum','date'));
        }

how i can fix it ?

0 likes
13 replies
jlrdw's avatar

I normally convert date to correct format for a query:

Y-m-d
COUPDEGRACES's avatar

im trying to find my data when from date 2021-17-03

Screenshot : https://prnt.sc/10ok09p

when im click button "Cari", then automated to 1970-01-01

Screeenshoot: https://prnt.sc/10ok31j

My database

Screenshoot : https://prnt.sc/10ok2ak

This form datepicker im using

<div class="card-deck">
  <div class="card">
    <div class="card-body mb-2">
    <form action="{{ route('laporan.check') }}" method="POST">
    @csrf
      <h5 class="card-title">Cari /Tanggal</h5>
      <p class="card-text">
      <input id="datepicker" width="320" name="date"/>
      <br>
      <button type="submit" class="btn btn-primary">Cari</button>
      </form>
    <script>
        $('#datepicker').datepicker({
            format: 'yyyy-dd-mm',
            uiLibrary: 'bootstrap4'
        });
    </script>
      </p>

    </div>
    <div class="card-footer">
      <small class="text-muted">Last updated 3 mins ago</small>
    </div>
  </div>
jlrdw's avatar

The query is probably trying to find a 17 month, use correct date format in the query, MySql stores yyyy-mm-dd, double check this.

tykus's avatar

What do you receive for $request->date inthe controller; the following line probably is not correctly converting to unix time.

$date = date('Y-d-m ', strtotime($request->date));
COUPDEGRACES's avatar

still same sir, im change

 $date = date('Y-d-m ', strtotime($request->date));

to

 $date = date('Y-m-d ', strtotime($request->date));

In search.blade.php

    <script>
        $('#datepicker').datepicker({
            format: 'yyyy-mm-dd',
            uiLibrary: 'bootstrap4'
        });
    </script>

to

    <script>
        $('#datepicker').datepicker({
            format: 'yyyy-dd-mm',
            uiLibrary: 'bootstrap4'
        });
    </script>

But still give same error sir .

tykus's avatar

Can you dd($request->date) and post the result here?

tykus's avatar

That is not the result of dd($request->date).

Here's the issue I am expecting, the $request->date is already in the Y-d-m format, so

>>> date('Y-d-m', strtotime('2021-17-03'))
=> "1970-01-01"

This is because strtotime('2021-17-03') is false.

tykus's avatar
tykus
Best Answer
Level 104

Ok, so exactly as I described above; strtotime is returning false whenever it tries to handle that date string.

Just remove that line from your controller code; and pass $request->date directly into the query.

if ($request->date){
    $date = $request->date;
    $orders = DB::table('iklan')->where('waktupesan',$date)->get();
    $sum = DB::table('iklan')->where('waktupesan',$date)->sum('harga');
    return view('laporan.result',compact('orders','sum','date'));
}
tykus's avatar

Is the waktupesan date stored in the same format Y-d-m?

Please or to participate in this conversation.