Code snippets

You will find here 13 code snippets I find useful so I don't have to Google ™ or Stackoverflow ™ them when I need. There are also code snippets I always forget! 😁 Sometimes, related links to the code will be available. The main difference with other random snippets you will find on the web is that in most cases (for the Symfony related ones) you will be able to run them directly from this website to check the output so you can verify it works as expected ™.

This snippet shows three ways to create a date object from a timestamp with PHP. The trick with the @ prefix is not well known. No...

PHP

Published on 2024-02-27 • Modified on 2024-02-27

 View the code


This snippet show how to calculate the number of days until the end of the year with PHP. PHP correctly calculates the "z" format ...

PHP

Published on 2023-06-21 • Modified on 2023-06-21

 View the code


This snippet shows how to display a date interval with Twig. In fact, we can use the diff() function of the DateTime() class and t...

Twig

Published on 2023-04-20 • Modified on 2023-04-20

 View the code


This snippet shows how to get a DateTime object from a DateTimeImmuntable object with PHP. We can use the setTimestamp() function ...

PHP

Published on 2023-01-03 • Modified on 2023-01-03

 View the code


This snippet shows how to create a UTC timezone date with PHP. The first thing to know is that if your server already has UTC as t...

PHP

Published on 2022-11-10 • Modified on 2022-11-10

 View the code


This snippet shows how to compare two DateInterval objects. We can't directly use the spaceship operator on DateInterval objects o...

PHP

Published on 2022-07-13 • Modified on 2022-07-13

 View the code


In this snippet, we see how to calculate the number of months between two dates in PHP. Here, we don't care about the days at all....

PHP

Published on 2022-06-15 • Modified on 2022-06-15

 View the code


This snippet shows how to translate the day and month of a date with PHP. We use the intl extension and its IntlDateFormatter clas...

PHP

Published on 2022-04-12 • Modified on 2022-04-12

 View the code


This one is for me! I always want to write: "last day of the month" which sounds OK... but, it's not! It's "last day of this month...

PHP

Published on 2021-01-30 • Modified on 2021-01-30

 View the code


In this snippet, we will see how to remove microseconds from a PHP DateTime object. This was a breaking change from PHP 7.0 to 7.1...

PHP

Published on 2021-01-23 • Modified on 2021-01-23

 View the code