french_dev
french_dev

Reputation: 2177

bootstrap datetimepicker retuns a string, how to convert this string into datetime (php)

I have a bootstrap datetimepicker in a form with method POST.

In my Symfony2.7 application, I revover the $_POST['date'] like this:

$startDate = $this->get('request')->request->get('date');

I need this data to be a datetime, and only a datetime in order to use this in a doctrine dql. But when I make a var_dump($startDate); it returns me a string:

string '25/08/2015 00:00' (length=16)

Is there a way to convert this string into Datetime?

I try the method DateTime::createFromFormat but it retuns me false, of course because $startDate is a string.

There is my html code:

<div class='input-group date datePicker'>
    <input type="datetime" class="form-control" id="startDate" name="date" required="required"/>
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>

<script type="text/javascript">
    // load bootstrap datetimepicker
    $(function () {
        $('.datePicker').datetimepicker({
            locale: 'fr',
        });
    });
</script>

Upvotes: 0

Views: 609

Answers (1)

al&#39;ein
al&#39;ein

Reputation: 1727

Using DateTime as instance might do the trick. Its constructor deal with the string format.

$datetime = new DateTime($startDate);

And, as you stated in comments, replace the slashes before it:

str_replace('/', '-', $startDate)

Upvotes: 1

Related Questions