Matok's PHP Blog

Fun with PHP DateTime

article image

Hello, I wanna show you one easy trick with DateTime and modify method. Truth is, this isn't really a trick, but I saw many similar situation where a developer does not use simple approach and made bunch of useless code. So, back to the basics - read and learn how to use DateTime. You find out soon what to do and what you should not put into your code.

Construct a DateTime object

It's funny I'm going to write about this, but I saw several times use constructor like this:

// this is bad, don't use this:
$deadline = new DateTime($date.'00:00:00');

What is bad about this? Well, in theory nothing because it's working... but... When I see code like this it must read that one line at least five time to make sure I'm not dreaming. Why is time 00:00:00 there? Something change in new version of PHP... wait, no, developer just don't know it's implicit behavior. So I just lost 2 minutes thinking about this stupid line.

Please, construct your DateTime like below, because everyone who reads the manual knows.

// you don't have to put time info into constructor
// it's very strange if you working only with date further in code
$deadline = new DateTime($date);

Check formating string twice

From my perspective format method is bloody stupidity because of one-char format specification. Even experience developer makes easy error on this. Tricky one is formating character for minutes which is i. Watch carefully not to swap minutes i and months m.

I got funny story about this:

As you see nobody noticed this for 2 years (format on picture is d.m.Y H:m:s) and this was live project where developers were working daily...than Matok came on the scene and fixed it :P. Maybe I'll write article how to easy avoid formatting mistakes. So check my blog regularly.

Modify DateTime: last day of month

We have nice JavaScript Date (DateTime) Pickers ready to use... but sometimes you need to generate calendar in PHP, because it's more SEO friendly. For example - this is one we built as links to articles history: User can pick a date a see all published articles in chosen date.

Firstly, I want to post code that shows how not to do it, but it will be very long. Bad approach was something like explode('-', $date) and than j formatting char was used to get last date of month $lastDay = $date->format('j');... all wrong. The questing is: How do I print all days in any month?

$startDate = new DateTime($year.'-'.$month.'-01');
$endDate = new DateTime('last day of '.$year.'-'.$month.'-01'); 

echo 'Days in '.$startDate->format('m/Y').':'."\n";

for ($day = $startDate; $day <= $endDate; $day->modify('1 day')) {
    echo $day->format('d')."\n";
}

Yes, this is working. You can add links, some CSS and break line on new week. Easy calendar on few lines of code.

Conclusion

  • read manual how to use DateTime constructor
  • read manual how you can use modify method
  • and take care on formatting string

If you like this article then mark it as helpful to let others know it's worth to read. Otherwise leave me a feedback/comment and we can talk about it.

I'm foreigner. Where I live my friends call me Maťok.


Comments - Coming Soon