Adrian Enriquez
Adrian Enriquez

Reputation: 8413

How to pass javascript date to laravel 4 API timestamp

This is the format of my Javascript date using new Date()

2015-02-21T13:02:13.175Z

But the laravel API refuses it. Laravel timestamp is like

2015-02-21 12:55:06.

What is the best way to match them up, or to format my javascript date like laravel's timestamp. Should I do it in the client or server side?

ps: I can add date today in the server side but I need the javascript date because I'm using offline data that can be synced later. So I need the original date from the client side.

Upvotes: 2

Views: 1381

Answers (1)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

It is better to do in the server side. You should do date("Y-m-d H:i:s")

So you shall use in the frontend (However it will executed at server)

<?php
$DateTime = date("Y-m-d H:i:s");
//youroperationshere
?>

Note : This is the format supported by Laravel by default for created_at and updated_at columns itself.

Because the

You shall see the format accepted by laravel here

Update :

As the questioner needs to do the same in front end

function generateDateToday(){
    var d = new Date()
    var year = d.getFullYear();
    var month = ("0" + (d.getMonth() + 1)).slice(-2);
    var day = ("0" + d.getDate()).slice(-2);
    var hour = ("0" + d.getHours()).slice(-2);
    var minutes = ("0" + d.getMinutes()).slice(-2);
    var seconds = ("0" + d.getSeconds()).slice(-2);
    return year + "-" + month + "-" + day + " "+ hour + ":" + minutes + ":" + seconds;
}

So your output will be 2015-02-21 18:1:11 which laravel accepts

Upvotes: 1

Related Questions