Creating a simple dateTime object with the Symfony Alice fixtures bundle

Published on 2021-04-03 • Modified on 2021-04-03

In this snippet, we will see how to create a simple dateTime object with the Alice fixtures bundle. When you start to play with this bundle, you check the faker date providers, and you may try to use the dateTime() one and it doesn't work. The trick is to remember that you can use the native PHP functions like this:


App\Entity\myEntity:
    entity (template):
        # ramdom date in the current month with the Faker provider
        dateCreated: <dateTimeThisMonth()>

    # Force the date to be tomorow
    entity_{1} (extends entity):
        dateCreated: <date_create('tomorrow')>

    # Force the date to be yesterday
    entity_{2} (extends entity):
        dateCreated: <date_create('yesterday')>

    # Force with a given date
    entity_{3} (extends entity):
        dateCreated: <date_create('2021-04-03')>

    entity_{4..31} (extends entity): ~

 More on Stackoverflow   Read the doc  More on the web  Random snippet

  Work with me!