Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mohsen's avatar

Convert Time To TimeStamp

Hi I have date-picker ( js plugin) that sends the date as this format : 11/05/2016 I want to save that to time-stamp column in MySQL database ... how do I convert this date format to time-stamp in back-end side ?

0 likes
9 replies
jlrdw's avatar

If a date with no time on it, why not just use a date field? Read the mysql docs on date and datetime fields.

tisuchi's avatar

@Mohsen

Can try with that-

{{ Carbon\Carbon::parse($objectName->fieldName)->diffForHumans() }}
1 like
Snapey's avatar

If you want a timestamp;

$timestamp = \Carbon\Carbon::parse('11/05/2016')->timestamp;
3 likes
Mohsen's avatar

@Snapey

but this function insert 0000-00-00 00:00:00 in date column in database

 DB::table('test')->insert(
        ['date' => \Carbon\Carbon::parse('11/05/2016')->timestamp]
    );
willvincent's avatar
Level 54

@Mohsen Specify the format.. obviously carbon won't be able to determine if the second or third part of that date is month, and which is day.. instead of parse in this case you should use createFromFormat()

$timestamp = \Carbon\Carbon::createFromFormat('Y/m/d', '2016/05/05')->timestamp;

EDIT: You changed your date format in your post while I was replying.. but even so.. use createFromFormat to ensure the proper order of month, day, year is used by carbon.

5 likes
Snapey's avatar

You said you wanted a timestamp, but now a date? which is it?

Mohsen's avatar

@Snapey I want timestamp, date is the column name and timestamp is its type

EckyEckyPtang's avatar

@tisuchi Still spamming your own website infested with ads around stolen content... The one thing you 'wrote 'yourself isn't even a tutorial, it's just a line from the laravel docs with 5 ads around it. You are scum. Take your garbage elsewhere please.

Snapey's avatar

@tisuchi - wow, thats bad. most people will tolerate one or two ads for good content, but 6 adverts around very poor content? That really stinks.

Please or to participate in this conversation.