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

usamamashkoor's avatar

Laravel php and mysql datetime difference

Hi friend i am creating a chat system in php laravel mysql i want to show messages on the chat box related to datetime and i am using a timestamp and created_at in mysql for storing datetime but the problem i am facing here is that mysql timestamp and php created_at datetime is storing different datetime in database and i am using now() mysql function in mysql queries and also my local time is different to than the stored time. also i want to show when i send a message now from Pakistan and for you the message date time should display as 12/25/2015 11"04 PM and for me in USA the date time should display as 12/25/2015 12:05PM. Kindly please help me on this. Thanks an advance.

0 likes
13 replies
IsaacBen's avatar

If you're creating a chat then just use the timestamps and let Laravel store the date the regular way. You can then manipulate it with carbon to show it in the way you want.

For chat I recommend to use something like this

//It will show in a format like 5 days ago.
$table->created_at->diffForHumans();

To display in the format of 12/25/2015 you will do this:

use Carbon\Carbon; 
{{Carbon::parse($ticket->created_at)->format('m/d/Y')}}

So basically go read about carbon and see in what ways you can manipulate dates. It will solve pretty much all your date problems. https://github.com/briannesbitt/Carbon

usamamashkoor's avatar

@IsaacBen it is storing different datetime in mysql and my local time is different what should i do about that

IsaacBen's avatar

@usamamashkoor You should be able to specify your timezone at the config/app.php.

    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'UTC',
1 like
ohffs's avatar

I think you'd want to store the users timezone and then use carbons timezone localisation features to display the data?

1 like
usamamashkoor's avatar

@ohffs how can i include carbon in laravel 5.1 and also currently laravel is storing current datetime in created_at as i am beggenier can you please kindly guide me on next steps what should i do next to show local datetime on chat.Kindly please help me on it it will be really appreciated

ohffs's avatar

Carbon comes with laravel - so you are good to go :-) Very roughly you'd want a timezone on your user model - so in your migration you'd have something like :

  $table->string('timezone')->default('en'); // or whatever is appropriate for your users

Then when you are saving your users you would set something like :

$user->timezone = 'en';

On your message model you could have a little helper like :

public function localHumanDate() {
  \Carbon::setLocale($this->user->timezone);
  return $this->created_at->toDateTimeString();
}

Then when you are displaying the date in your view you would do something like :

{{ $message->localHumanDate() }}

I think that might work anyway! :-)

1 like
usamamashkoor's avatar

@ohffs so you are saying that i should add a colum of timezone of default value en in users table when i save the new user i should use this

 $user->timezone = 'en';

but i want to display local datetime on chat box from messages table also i am using angular ajax request json response to fetch data from database and show on the page the problem i am using ajax and fetching multiple rows in a json response from database and using angular ng-repeat for showing data on the page

ohffs's avatar

Ahhhhh - right. If you're using javascript/ajax it could be that you're better off storing a separate column on the messages which is a UNIX timestamp and then just using Javascript's date functions to localise & format it based on the current user.

1 like
usamamashkoor's avatar

@ohffs can you please tell is this is the unix 2015-12-25 17:36:01 uinx time stamp i am using time stamp to store this datetime on a separate column if it is then can you please tell me how how i can convert it to local time using javascript or jquery

ohffs's avatar
ohffs
Best Answer
Level 50

It would be a 'proper' UNIX timestamp - like '1451140206'. You can get it from php using the time() function - like (when saving a message) :

$message->unix_time = time();

In javascript you can convert it into a date object :

var date = new Date({{ $message->unix_time }} * 1000); // * 1000 as JS works in milliseconds

Then do something like :

print date.toLocaleDateString();

You'll probably want to make some kind of angular function to do the conversion I guess. You'll have to do a little searching for formatting the date in JS as I don't think the toLocaleDateString does quite what you want - there's maybe an angular library out there - I'm not sure.

1 like
usamamashkoor's avatar

Thanks for being so patient with me i really appreciate it Thanks again sir @ohffs i will try my best on it

1 like
ohffs's avatar

No worries - hope you get it working :-)

2 likes

Please or to participate in this conversation.