D
D
Dmitry2020-12-29 00:48:38
Laravel
Dmitry, 2020-12-29 00:48:38

How to still set dates/times in laravel?

The question is hackneyed, of course. But finally got confused.

There is a default config.app UTC
Time in homestead UTC.

Datepickers keep getting pushed back by 3 hours. If you change in the config to Moscow, then in general 6 hours of difference.

Already shoveled a bunch of things. But even more confused. It turned out dozens of some kind of transformations through casts dates then on the client ... a fierce vinaigrette (

How to streamline everything? I uploaded utc to the hosting there too. I had to return everything in the development environment. Is there any simple algorithm?

Align through accessors / mutators I'm not even so much worried about created / updated, but already mine - the start and end times of events.

In a DB tested and on datetime fields and timestamp set. One result. Rolls back 3 hours and that's it. This time is not given to me (I would be grateful

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pLavrenov, 2021-01-04
@pLavrenov

Try to pass dates, everywhere, in ISO8601 format in the database as it is, you must use UTC (otherwise it will turn into a problem in the future). Moment and Carbon know how to work with this format.
Most likely, when you test, the time server is transmitted which is on the computer. If you pass ISO8601, then it will automatically convert to UTC. The problem is that the 'Ymd H:i:s' format does not transmit the time zone. In general, you just need to take this as a standard and use such a scheme in every project.
Well, make a validator on iso. (weird it's not in the box yet)

Validator::extend('iso_date', 'App\Validators\[email protected]');

class IsoDateValidator
{
    public function validate($attribute, $value, $parameters, $validator)
    {
        if (is_array($parameters) && in_array('utc', $parameters)) {
            $regex = '/^(\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)))?$/';
        } else {
            // 2012-04-23T18:25:43.511Z
            // Regex from https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
            $regex = '/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/';
        }
        return preg_match($regex, $value) ? true : false;
    }
}

S
Sanes, 2020-12-29
@Sanes

Depends how you save. The database takes time which is in the Laravel config.
If you watch the database through PMA, then there is most likely a different time. Server time.
And JS takes time from the client. And in theory, nothing should be formatted.
In general, this is a must see. You probably won't find the answer here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question