I have Carbon Object Date which I change the time for a start_date and end_date, what happens is it both becomes the same thing even though I saved it on a seperate variable.
Input
$this->temp_mon_start = $date->setTime(8,0);
$this->temp_mon_end = $date->setTime(3,0);
dd($this->temp_mon_start, $this->temp_mon_end);
Output
date: 2021-11-15 03:00:00.0 Asia/Singapore (+08:00)
date: 2021-11-15 03:00:00.0 Asia/Singapore (+08:00)
It appears it saves the last setTime on the $date variable, which I don't know what is causing the issue. I'm using Laravel and Carbon. Is there any possible alternative on this? Appreciate the help.
Well Carbon works that way. It does not create a new instance but rather changes existing one.
So insted of using:
$this->temp_mon_start = $date->setTime(8,0);
you can use:
$this->temp_mon_start = $date->copy()->setTime(8,0);
copy() method creates new instance of Carbon object.
Related
So,
I know a lot of requests and question has been askeb about this subject but none really worked for my case... I'm working on a liscensing api with php (supposed to be easy) and I get a string date (2000-01-01) from my db and the length of the subscription. So I'm creating a DateTime Object with it using this :
$created_at = date_create($result["created_at"]);
date_add($created_at, date_interval_create_from_date_string($result["length"]." days"));
But for some unknowed reason, It seems I can't get the current date in a DateTime object so I can just compare them with <>=. Even if I use date_sub() or date_diff() It still require two DateTime object. I'm really deseperate at this point so I figured I could ask for some help.
Hope I didn't miss anything obvious
You can use the 'now' attribute,
$today = new DateTime('now'); to get the current time.
Don't forget to set your timeregion in your php.ini to be able to get the right time.
And if you want to compare them, you can use date_diff and then
$var->format('%r') to get the value.
%r is going to be empty if the result is positive.
Good luck!
I am getting dates from an API call. The date is formatted in this way
2017-10-19T15:30:00
I want to store this date in my MYSQL database using Laravel Database Migration, currently I am using
$table->dateTime('datetime');
When I store it using a dateTime field as above, all I get is
0000-00-00 00:00:00
When I use a timestamp format, I don't get accurate dates, I just get the current time and date.
How can I solve this? Any help would be appreciated, and please let me know if you want further information.
Luckily, Laravel uses the Carbon class, which makes things a lot easier to modify dates. In your case, you want to do this:
Carbon::createFromFormat('Y-m-d\TH:i:s', $date);
There are two ways you can implement it: you can modify it before you save it to your database, or you can add a mutator on your model.
public function setDatetimeAttribute($value)
{
$this->attributes['datetime'] = Carbon::createFromFormat('Y-m-d\TH:i:s', $value);
}
You may want to build in some validation to see which format the date/time is in before you try to convert it.
in the model you should put:
protected $dates = ['datetime'];
Use Carbon
$dt = Carbon::parse('1975-05-21 22:23:00.123456');
to save:
$model = new Model;
$model->date = $dt; // you can use the carbon object directly
$model->save();
I've got a variable declared like such: $var = new DateTime(null);.
I want to change the time that echo $var->format('g:i A'); outputs.
I want to achieve this solely by using a time string such as 07:30:28.
How can I do this without re-creating a DateTime object ($var) each time? I can't think of a way to achieve this.
DateTime::modify() will do exactly what you want. It will quite happily accept a time in string format and apply it to the object:-
$date = new \DateTime();
$date->modify('07:30:28');
See it working.
Alternatively, you can do it all in one go:-
$date = (new \DateTime())->modify('07:30:28');
i'am trying to format a mysql datetime-value to another format for the view.
I tried several methods, for example:
$date = new DateTime($datetime, new DateTimeZone('Europe/Berlin'));
return $date->format("Y-m-d H:i");
But it doesn't work.
The problem is, that (i think..) PHP manipulates the value. The output is always the current datetime and not the value which is saved in the database. For example "2011-04-21 22:27:42".
Does anyone has an idea how the solve this problem?
Greetings.
My guess is that $datetime isnt set
The first parameter of DateTime is defaulted to now if it's not set
http://www.php.net/manual/en/datetime.construct.php
I create an object
$date = new DateTime();
It is set to current date 2011-04-01 21:43:40. I try the following
$date->modify('midnight');
I expect the object to set to 2011-04-01 00:00:00. But nothing happened. Object hadn't beed modified, and continue to have a 2011-04-01 21:43:40 date. I just want to reset the time to midnight (00:00:oo).
http://codepad.org/w5RAF0Lh
This piece of code (with midnight) will not work without date.timezone setting
UPDATE: this piece of code requires PHP 5.3.6 to work correctly. In previous versions DateTime::modify('midnight') didn't work
Got a few questions, perhaps the will help illuminate the problem...
Is a timezone set in your php.ini file?
After you create the new DateTime() object are you using var_dump() or some other function to view its parameters and get the set date?
Have you tried and been successful passing other date and time formats into the modify method?
Doctrine checks if the DateTime object has changed its reference.
Modifying an object doesn’t change the reference, so for doctrine, this is not a change.
Use new \DateTime('midnight') instead.
I had the same problem!
However the returned date was correct, so what I did is:
#$date->modify('midnight');
Solved using
$date = new DateTime(date('Y-m-d H:i:s', strtotime('today midnight')));
echo $date->format('Y-m-d H:i:s');