I have a date that is coming from database (type date):
2018-08-25
This date is in french timezone.
When I do:
$from = Carbon::parse("2018-08-25", 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-24 22:00:00.0 UTC (+00:00)
Which is what I want
But when I use field from DB:
$operation = Operation::findOrFail($request->operation);
$from = Carbon::parse($operation->date_ini, 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-25 00:00:00.0 UTC (+00:00)
In my DB, field is saved as : 2018-08-25, so literraly, it means 2018-08-25 UTC. So result is coherent. But I'm not sure how to deal with it to get what I want. The implication would be that I have to store my date like a datetime in DB so that I can store it in UTC with 1 or 2 hours less. Is there anyway to avoid this and keep it simple ?
Any idea ?
I solved it using:
$from = Carbon::parse($operation->date_ini)->shiftTimezone('Europe/Paris');;
shiftTimezone with change timezone without changing the date. So, it do the trick for me !
If you call setTimezone() on an existing instance of Carbon, it will change the date/time to the new timezone, for example
$changeTimeZone = \Carbon\Carbon::parse($operation->date_ini)->setTimezone('Asia/Dhaka')->format('H:i');
Related
I work on a course system that has course times I want when the site is opened in a country, the time of the course appears for the user in the real time of the country in which the user is located
To convert user time to UTC use following laravel code in your controller method
# convert user date provided date string to user date
$user_date = date('Y-m-d H:i:s', strtotime($date_string));
# convert user date to utc date
$utc_date = Carbon::createFromFormat('Y-m-d H:i', $user_date, $user->timezone);
$utc_date->setTimezone('UTC');
# check the utc date
dd($utc_date);
Convert UTC time to User time
# using utc date convert date to user date
$user_date = Carbon::createFromFormat('Y-m-d H:i', $utc_date, 'UTC');
$user_date->setTimezone($user->timezone);
# check the user date
dd($user_date);
I am making api's in laravel and getting 2021-01-30T10:30:17.704 05:30 from $request->followup and i have column in database named followup having datetime datatype. But it's giving me following error.
Carbon\Exceptions\InvalidFormatException: Could not parse '2021-01-30T10:30:17.704 05:30': DateTime::__construct(): Failed to parse time string (2021-01-30T10:30:17.704 05:30) at position 24 (0): Double time specification in file D:\xampp2\htdocs\synocrm-baid\rest-apis\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php on line 188
I tried to change format like
$followupDate = date('Y-m-d h:i:s A',strtotime($request->followup));
My modal having
#property Carbon|null $followup
$request->followup is appending datetime with datetime
Original Value: 2021-01-30T10:30:17.704 05:30
Datetime: 2021-01-30T10:30:17.704
Time: 05:30
As we can see, there is two Time (05:30 and 10:30:17), because of which the strtotime() cannot convert the value.
In order to fix the problem,
Send the value as time time only (2021-01-30T10:30:17.704) instead of two times Time attribute
If you have no control over request value, you can retrieve only date value like:
$dateValue = explode(' ', $request->followup)[0];
$followupDate = date('Y-m-d h:i:s A',strtotime($dateValue));
Or Using Carbon
$followupDate = Carbon::parse($dateValue);
I would not recommend option 2 because it creates problems and confusion for other devs.
2021-01-30T10:30:17.704 05:30
It does not seem to valid timestamp. For parsing, it should be the valid timestamp or date.
I'm setting my time using javascript when creating/updating my cookie.
// get current time
let d = new Date();
let time = d.getTime();
Which variable time becomes this string...
1586186947954
The number above should result 16:29 GMT 6 April 2020
But when I run this number with carbon...
use Carbon\Carbon;
$updated = Carbon::parse($cart['updated']['time']);
This is returned...
Carbon\Carbon Object
(
[date] => 52234-04-07 05:32:34.000000
[timezone_type] => 1
[timezone] => +00:00
)
Which is way off for reason. Minutes and the date are no where near.
I'm trying to output this timestamp in the timezone Asia/Dubai too if I can get Carbon to return the right time anyway.
Any ideas would be great thanks.
The parse method is for parsing more complex strings, so it is misinterpreting the value you're passing.
You should instead construct the object like this:
Carbon::createFromTimestampMs(1586186947954, 'Asia/Dubai')
I have a date stored as follows:
$user->password_expiry_dt
Carbon #1573228178 {#647 ▼
date: 2019-11-08 15:49:38.0 UTC (+00:00)
}
I want to check if its expired without using the timestamp so currently i'm doing this:
Carbon::parse($user->password_expiry_dt)->hour(0)->minute(0)->second(0) <= Carbon::today()
Is there an easier or more elegant way to get the date without the timestamp as follows without having to chain all these extra setters?
2019-11-08 00:00:00.0
I think Carbon::parse($user->password_expiry_dt)->startOfDay() will do it for you
I've this document stored in mongo:
{
"_id" : "cpd4-734fc2db-a5b0-4881-b5d7-bf85d894178d",
"expiresAt" : ISODate("2018-10-10T00:00:00Z")
}
In order to get sure, all data is right, I've got first the document and I've log some data:
Reference ref = this.mongoTemplate.findById("cpd4-734fc2db-a5b0-4881-b5d7-bf85d894178d", Reference.class);
LOG.info(ref.getExpiresAt().toString());
LOG.info(Long.toString(ref.getExpiresAt().getTime()));
The result is:
Wed Oct 10 02:00:00 CEST 2018 <<<<<<<<<<<<<<<<<<<
1539129600000
As you can see when I get the object, the expiresAt field is 02:00:00 instead of 00:00:00.
Value in database is expiresAt field is: ISODate("2018-10-10T00:00:00Z")
Any ideas or thoughts welcome for this issue!
This date is in Zulu time (note the 'Z' on the end):
ISODate("2018-10-10T00:00:00Z")
When you do this, specifically the call to .toString(), you are converting the date into a local date string in your time zone, which appears to be Zulu+2:
LOG.info(ref.getExpiresAt().toString());
I usually set my server's time zone to UTC/Zulu/GMT, in order to avoid any automatic timezone conversions happening like this.