Laravel 5 generation carbon date time with timezone - laravel-5

I have publish ERP site in cloud platform and I want to generate datetime for user assigned timezones.
I am using carbon datetime library to convert. But its gave me bellow issue when i am converting.
$carbon = new Carbon();
$carbon->setTimezone('Asia/colombo');
echo $carbon; //output is : 2016-07-06 22:33:05 | Asia/colombo
BUT
echo $carbon->now() // output is : 2016-07-06 17:03:05 | UTC
Why $carbon->now is giving wrong timezone even I have set timezone in carbon object also....
sorry If I am thinking wrong way.........

Carbon::now is a static function, which you invoke on your object but it is still a static function. There is no underlying object to pull the timezone information from. You need to do this: Carbon::now('Asia/colombo')

If you use Carbon's setTimezone() function on an object, you should only call non static functions on that object.
After manually applying a timezone, just use the non static output functions.
Some examples:
// without parameters it is now() !
$carbon = new Carbon\Carbon();
$carbon->setTimezone('Asia/colombo');
echo $carbon->toDateTimeString()."<br>";
// custom output format
echo $carbon->format('d.m.Y H:i:s')."<br>";
$carbon->setTimezone('Europe/Berlin');
echo $carbon->toDateTimeString()."<br>";
// custom output format
echo $carbon->format('d.m.Y H:i:s')."<br>";
// or pass something compatible to http://php.net/manual/en/datetime.construct.php
$carbon = new Carbon\Carbon('24.12.2015 20:13');
$carbon->setTimezone('Asia/colombo');
echo $carbon->toDateTimeString()."<br>";
// custom output format
echo $carbon->format('d.m.Y H:i:s')."<br>";
$carbon->setTimezone('Europe/Berlin');
echo $carbon->toDateTimeString()."<br>";
// custom output format
echo $carbon->format('d.m.Y H:i:s')."<br>";
The static methods will use the timezone defined in your config/app.php ( default: UTC)

Related

Converting String Values into Date using Carbon Laravel

The sql field type is VARCHAR, I am saving date and time from an API. The format that I get is 2018-04-28T22:17:41+05:30. Now I need to convert this using carbon to get a format like 04 April 2018 and timings. I couldn't convert varchar filed value into date format which I needed.
And the expected format should be passed to view, I did that using ( $ticket->dateOfIssue and it's giving this - 018-04-28T22:17:41+05:30 ). But as I said, I need the expected format (which is 04 April 2018, time ).
You should either use Accessor on your model or set the datetime format on your model. The example of the former is:
use Carbon/Carbon; before your class declaration
public function getDateOfIssueAttribute($value) {
return Carbon::parse($value)->format('d M Y , H:m:s');
}
When ever you retrieve this model anywhere you already have it in the format you set in your accessor.
Here is an example to parse the date
Carbon::parse('2018-04-28T22:17:41+05:30')->format('dd MM YYYY');
Moreover, do not forget to import the Carbon namespaces on the top
You try
use use Carbon\Carbon; // on top
$time = Carbon::parse('2018-04-28T22:17:41+05:30')->format('d M Y'); //28 Apr 2018
Good luck
This is the exact format for date and time. First of all include carbon library in your controller
use Carbon\Carbon;
class abc extend Controller
{
public function cancell()
{
$ticket = Booking::all()->where('status', '=', 'CANCELLED');
$dateOfIssue=$ticket->dateOfIssue;
$time = Carbon::parse($dateOfIssue)->format('d M Y , H:m:s');
return view('Admin.Tickets.cancelledTickets')->with('ticket', $ticket);
}
}
If you have multiple records then you can use loop for that
Please try ,
\Carbon\Carbon::parse('2018-04-28T22:17:41+05:30')->format('d- M- Y');
the output is 28- Apr- 2018
using sql
DATE_FORMAT(mydate, '%d-%M-%Y')

Laravel: Check if time + 4 hours has passed

I store in my database a date format like this:
2017-02-22 16:55:40
I added it to my database like this:
Carbon::now();
I need to check if 4 hours passed since this date.
How I can do this? I couldn't figure out how I can convert this format into Carbon or timestamp.
If you are using Laravel and the date is a Carbon instance from a Model you have access to the whole Carbon API.
You can use the Difference API of Carbon for this specific purpose.
echo $model->thedate->diffInHours($now, false);
If your model does not threat the date as a carbon instance you can cast it by adding the date to the dates array of the current model like so
protected $dates = [
'field_name',
];
Check out Date casting for more information
Update with an explicit example
$user = User::first();
// This will return the difference in hours
$user->created_at->diffInHours(Carbon\Carbon::now(), false);
You can convert it to a Carbon object with:
Carbon::parse('2017-02-22 16:55:40');

Create sane start and end dates with nelmio\alice

I want to create a completedDatetime that follows the datetime in the orderDatetime field.
Fixtures.yml
directive_{200..500}:
orderDatetime: <dateTimeThisYear()>
completedDatetime: '90%? <dateTimeBetween("orderDatetime", "now")>'
I used the code above in my fixtures file and got the data that follows.
Is there a way to ensure a sane result using faker data short of writing custom functions in LoadFixtures??
Since you are passing string "orderDatetime" to strtotime() function it returns 1970 year and your dateTimeBetween works like dateTimeBetween('1970', 'now'). Passing of variables is done with $orderDatetime. But if you pass such variable to dateTimeBetween(), the future dateTime might get passed which is not possible.
It only works if your order date is the past from now:
orderDatetime: '<dateTimeBetween("-200 days", "now")>'
completedDatetime: '90%? <dateTimeBetween($orderDatetime, "now")>'
If you wonna generate future dates then you need to create custom faker function for example:
public function dateTimeBetweenAfter($dateFrom, $dateTo)
{
$date = new DateTime($dateFrom->format('Y-m-d H:i:s'));
$dateTo = $date->modify($dateTo);
return FakerDateTime::dateTimeBetween($dateFrom, $dateTo);
}
and use it like this:
orderDatetime: <dateTimeThisYear()>
completedDatetime: '90%? <dateTimeBetweenAfter($orderDatetime, "+1 year")>'

How to save date in Laravel in GMT 0?

How to save date in GTM 0 in database?
I mean this: protected $dates = ['created_at'];
I'd personally recommend leaving/setting the timezone in your app config as UTC rather than GMT:
https://www.timeanddate.com/time/gmt-utc-time.html
To make sure my custom date fields are stored correctly I use a modifier on my model class like so:
public function setStartAtAttribute($date)
{
$timezone = (Auth::check() ? Auth::user()->profile->timezone : config('pgn.phoenix.timezone'));
$tzdate = Carbon::createFromFormat('d/m/Y H:i', $date, $timezone);
$this->attributes['start_at'] = $tzdate->timezone('UTC');
}
That would need use Carbon\Carbon; adding to the top of your model too.
That config param above is set to 'Europe/London' and is the timezone that our guests are shown dates, (works with daylight savings) whilst not affecting the 'system' timezone.
You can change the timezone of the application by going to the config/app.php file and changinf the following line:
'timezone' => 'GMT'
If your dates are still saved in a different timezone, it is likely that the server datetime will need to be altered to GMT. This can be done using the following command and following the instructions:
dpkg-reconfigure tzdata

Convert unix timestamp to normal Date in Json

I am using laravel 5.0. I am getting data from controller as json. I am having value like this.
{"timstamp_date":"1434360957"},
I need to convert this unix timestamp value as Normal Date Like (15-06-2015) or (15-March-2015).
I have used Date(timstamp_date) but it is showing current time only. Not my timstamp date
You could use:
date("d-m-Y H:i:s", 1434360957);
EDIT
You could try;
var dateTime = new Date(1434360957*1000);
var formatted = dateTime.toGMTString();
https://jsfiddle.net/sp57Lnpf/
Use the date function. You need to specify the format as the first parameter:
date("d-m-Y", $timestamp_date)
http://php.net/manual/en/function.date.php
Laravel also comes with Carbon you could use that if you wanted to for further manipulation of the data if you so required it.
http://carbon.nesbot.com/docs/

Resources