Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
285
Q:

date("d.m.Y.", strtotime()); - Getting Incorrect Date

Hello everyone,

I'm trying to use the `date()` function in PHP to display the current date in the format "d.m.Y.", but for some reason, I'm getting the incorrect date. I have used the `strtotime()` function to generate the timestamp for the current time, which should be passed as the second argument for `date()`.

Here's the code I'm using:
```php
date("d.m.Y.", strtotime());
```

However, when I run this code, instead of getting the current date, I'm getting a different date altogether. I have checked the server time, and it seems to be correct. I'm not sure what I'm doing wrong here.

Can anyone please help me figure out why I'm getting an incorrect date with this code? Am I missing something obvious in my syntax or understanding of these functions? I would appreciate any guidance or suggestions.

Thank you in advance for your help!

All Replies

gibson.seth

Hey there,

I had a similar issue in the past, and I managed to resolve it. The problem you're facing might be related to the second argument of the `strtotime()` function. When you pass an empty argument to `strtotime()`, it doesn't generate the timestamp for the current time.

To get the current timestamp, you need to pass the keyword `'now'` as the argument. So, your code should look like this:

php
date("d.m.Y.", strtotime('now'));


By making this change, the `date()` function should now display the correct current date.

I hope this helps! Let me know if you have any further questions.

adrianna.hauck

Hi everyone,

I encountered a similar issue before, and it took me a while to figure out what was causing the incorrect date. After analyzing the code you shared, I noticed that you are missing a required argument in the `strtotime()` function.

To get the current timestamp, you need to provide the keyword `'now'` as the argument. However, in your code, you're not passing anything to `strtotime()`, which results in an unexpected date being displayed.

To fix this, modify your code like this:

php
date("d.m.Y.", strtotime('now'));


By explicitly specifying `'now'` as the argument within the `strtotime()` function, you'll ensure that the correct timestamp is generated, and the `date()` function will display the accurate current date.

I hope this helps in resolving your issue. Feel free to ask if you have any further questions or need additional clarification.

Best regards!

New to LearnPHP.org Community?

Join the community