-2

I am just beginner in Laravel and as I watching certain tutorials on YouTube and author to change format of created_at and updated_at in the view changed from string to unix timestamp and would like to ask why we need to convert date from string to unix timestamp with the use of strtotime function. Can't we still use string rather than converting it to unix timestamp.

Secondly, why exactly unix timestamp over date time. Are there any advantages of unix timestamp over date time

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
Mirasan
  • 259
  • 1
  • 4
  • 16
  • Please do not abuse the formatting tools. – John Conde Sep 05 '18 at 14:10
  • 2
    You don't have to use `strtotime()`. In fact, you shouldn't use it as it is not the right tool for the job. See [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php). – John Conde Sep 05 '18 at 14:11
  • 2
    If your `created_at` and `updated_at` values are attached to a Model (like `User`), they are already `Carbon` values (https://carbon.nesbot.com/docs/), so using `strtotime()` is redundant. You can just use `$user->created_at->format(...)` and print the date however you see fit. – Tim Lewis Sep 05 '18 at 14:12
  • @TimLewis, hey Timmy, so if I want to change the format of created_at and updated_at I can just use format(...) function, that is, function('Y-m-d H:i:s') Right? Look dude I just want your advice as you know when you create new table then it will automatically have timestamps as created_at and updated_at. Which way would you use if you wanted to change the format of both in the view? via format()? – Mirasan Sep 05 '18 at 14:36
  • 1
    Depends. If you're using `DB::table("users")->get()` vs `User::get()`; One will be a `Collection` of `stdClass` objects, while the other is a `Collection` of `User` models. Only the model converts `created_at` and `updated_at` to `Carbon` dates, so then you'd use `$user->created_at->format("F jS, Y")` (http://php.net/manual/en/function.date.php for syntax), otherwise you'll need to use `date("F jS, Y", strtotime($stdClass->created_at))` – Tim Lewis Sep 05 '18 at 14:40
  • @TimLewis, ok Tim, and how about User::all() and User::Find() they both also return collection of user models? if yes I gotta use date("F jS, Y", strtotime($stdClass->created_at)) and for them Also, Tim what do you mean by collection of stdClass just what it means in simple words. :) – Mirasan Sep 05 '18 at 17:00
  • Lol... too many questions... `User::all()` and `User::find()` don't return the same thing; one has a Collection of `User` models, the other is a single `User` model. `Collection` is an array-like helper in Laravel, `stdClass` is a PHP object, so literally a `Collection` of `stdClass` objects. Read some more tutorials, documentation, try examples, and you can figure this all out on your own. – Tim Lewis Sep 05 '18 at 17:04
  • @TimLewis, alright Tim sorry about numerous questions asked esp after yesterday's headache caused :) you know what you are different from majority of those experts who sit in stackoverflow since oftentimes people just not reply back or just ignore questions but not you thus you are cool :D. Just my sincere words :) – Mirasan Sep 05 '18 at 17:11
  • 1
    I'm glad to help, but I too have a limit lol. Keep working with Laravel though, things will make sense eventually. And yeah, try to keep the questions to a single, specific issue :P Getting into concepts and how they relate to each other tends to be too broad. – Tim Lewis Sep 05 '18 at 17:14

1 Answers1

2

You do not need to use it unless you are required. You will know the reason if you ever required it.

The purpose of the function as per the PHP manual is,

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

In summary, it converts the date given in English to the number of seconds in Numeric.

Muneer
  • 7,384
  • 7
  • 38
  • 62
  • yeah I have already read it but cannot understand the benefits of conversion to unix timestamp – Mirasan Sep 05 '18 at 14:19
  • 2
    @Mirasan The main benefit of a Unix timestamp is it's the same everywhere, regardless of timezones. Having a date in Unix timestamp format means you can present it in any timezone you like, whereas a date like `2018-09-05 10:27:38` could take place anywhere in a 24 hour period. – ceejayoz Sep 05 '18 at 14:27