Carbon::now returns wrong month - laravel

I'm trying to make cron job for my system.
The project is configured for time zone that differs from mine
I tried
$timezone = date_default_timezone_get();
$current = Carbon::now(new \DateTimeZone($timezone));
It returns 2018-01-05 06:36:15
which had a wrong month
I tried $current_m = date('y-m-d h:m'); which returned
18-02-06 06:02
which had right date but wrong time
what should I do? I need to be using carbon $ I'm using laravel 5.4

The problem wasn't in Carbon, It was in using $current->subHours(48) several times.

Related

cookie laravel s

Let's say I have a like field in my model, now I want to make the functionality of adding a like using Cookie. For example, we have an article ID, we check in cookies, if there is no data, then we add like and set the time for 10 hours, for example, and so on, 10 hours pass, we check and again we can add like and set this time again. If the data is already there, then we do not add like and do not update the time.
Can anyone see some examples of this? Or where to start and how to implement it?
I read the documentation but did not quite understand how it should work
Let's get a value with a like field
$likes = Request::cookie('like');
Choosing the time
$minutes = 600;
And what's next, how can I make a check to add a like?
Use Replace function on the string and replace " )." with ")."

Functionality of likes using cookies

Let's say I have a like field in my model, now I want to make the functionality of adding a like using Cookie. For example, we have an article ID, we check in cookies, if there is no data, then we add like and set the time for 10 hours, for example, and so on, 10 hours pass, we check and again we can add like and set this time again. If the data is already there, then we do not add like and do not update the time.
Can anyone see some examples of this? Or where to start and how to implement it?
I read the documentation but did not quite understand how it should work
Let's get a value with a like field
$likes = Request::cookie('like');
Choosing the time
$minutes = 600;
And what's next, how can I make a check to add a like?
If your updated_at column is not updated automatically on update (Eloquent do this by default), you need to do it, so the query can filter the row with correct time for update. You can use Carbon for easy time calculations.
$likes = Request::cookie('like');
$hours = 10;
if($likes) {
# Eloquent way
Article::find($id)
->where('updated_at', '<', Carbon::now()->subHours($hours)->toDateTimeString())
->increment('like');
}
If you want Query Builder way use this syntax:
# Query Builder way
DB::table('articles')
->where('id', $id)
->where('updated_at', '<', Carbon::now()->subHours($hours)->toDateTimeString())
->increment('like');
Not tested.

How to get time in particular timezone in Laravel?

I am unable to get the time for a particular timezone using Laravel 8. I have tried the following:
Carbon::now(new \DateTimeZone('Indian/Mauritius'))
I have also tried to modify the timezone in file config\app.php as follows:
'timezone' => 'Indian/Mauritius'
It is always returning me the UTC time. How to get the time using Carbon in that specific timezone?
Edits: issue solved, the problem was because of a serialization issue, check here
This works for me:
$dateNow = \Carbon\Carbon::now();
$dateNow->setTimezone('Indian/Mauritius');

Laravel - Use timezone manually without relaying on server timezone configuration

I'm using laravel and I have problem with the server time configuration.
That means that the no matter what timezone I set laravel configuration to, The server time will always be 1 hour and 12 minutes ahead of the current timezone.
My question is - there any temporary/quickfix I can make in order to get the right hour and minute without relaying on the server timezones which are incorrect?
I know this is not problem of Laravel, but I need some quick fix ideas until I'll set up a new server or fix this issue. Any way to decrease 1 hour and 12 minutes to the laravel timezone?
If you have the difference of 1, 2 or 3 hours, you can change your timezone to get it right but with 1h12 there's no way to have a configuration of laravel to do that.
So, we'll use php to do that for you. Create a helper file helpers.phpunder app/. In this file, create the following function:
function getCurrentTime()
{
return date('Y-m-d H:i:s', time() - 4320);
}
Then, you must config your composer.json to autoload your helpers.php:
"autoload": {
...
"files": [
"app/helpers.php"
]
...
}
and you must run composer dump-autoload to make it work.
From now, you can call getCurrentTime() at any moment, it'll give you the time you desire.

Server time in Joomla

Do you know of any way to check the current time in Joomla considering the selected time zone in global configuration?
I have been looking in administrator settings but did not see any single word about the current time.
You can use the following code:
$date = new DateTime();
$config = JFactory::getConfig();
$date->setTimezone(new DateTimeZone($config->get('offset')));
This does two things. The first line creates a DateTimeobject based on the current server time and timezone. The following two lines get Joomla's global configuration and change the timezone of out date object to that timezone.
You can then format the date to whatever format you like by calling $date->format('Y-m-d H:i:s');. You can replace the format specifier by whatever you need, a reference of possible formatting options can be found here: http://www.php.net/manual/de/function.date.php

Resources