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

lrf002's avatar

Why does my laravel controller change tha date when I use date() function?

In my android app, I get the current date like this:

LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = date.format(formatter);

After that, I send that date with more information to my laravel web page using Retrofit2. To store that date in the date column in my database use date() function and I do it like this:

$fecha = date('Y-m-d',strtotime($datos1Array['fecha']));

$datos1Array['fecha'] is the attribute that has the date. The problem is that when it is stored in my database, it´s stored like: 1970-01-01 and not the date I sent from the android app which is the current date. I have debug the code in the android app to check if the current date wasn´t right, but it is. So I don´t know why that happens. Any idea? Thank you very much.

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue is likely with the format of the date being sent from the Android app. The Laravel date() function expects a date string in the format of "Y-m-d", but the date being sent from the app is in the format of "dd LLLL yyyy".

To fix this, you can update the date format in the Android app to match the expected format in Laravel. Change the line:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");

to:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

This will format the date string in the correct format for the date() function in Laravel.

Alternatively, you can update the Laravel code to parse the date string in the format being sent from the app. Change the line:

$fecha = date('Y-m-d',strtotime($datos1Array['fecha']));

to:

$fecha = date_create_from_format('dd LLLL yyyy', $datos1Array['fecha'])->format('Y-m-d');

This will parse the date string in the format "dd LLLL yyyy" and then format it as "Y-m-d" for storage in the database.

vincent15000's avatar

If you have this format dd LLLL yyyy, you need to create a date from this format with Carbon.

$date = \Carbon\Carbon::createFromFormat($format, $time, $tz);

This will transform the sended format to the needed format to be stored in the database.

It would probably be better to use a custom setter in the model to do this transformation.

JussiMannisto's avatar

1970-01-01 00:00:00 is known as the Unix epoch. Most modern operating systems count time with an integer starting from that point, one unit per second. If you're getting that output from date(), that means the timestamp parameter you gave was zero or some other falsy value.

In this case it's clear that strtotime() cannot parse the string you're giving it, and it returns false. That gets interpreted as zero in by date() and you get 1970-01-01.

I don't recommend sending dates in that format from your Android app. It's a very locale-dependent format and it may not be parseable by the PHP backend. strtotime() may understand "21 April 2023" but not the date in some other locale. This is why you're getting 1970-01-01.

I recommend you send dates to your server in some locale-agnostic format, e.g. 2023-04-21. This also makes the date validation much easier. But remember that time zones also affect dates. Usually you'd want to send a full timestamp with timezone information and then convert that to a date on server, just as you're doing. This corrects any time zone differences between the client and the server.

Please or to participate in this conversation.