Modify DateTime object - 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');

Related

setTime Carbon also affexts other variable

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.

Php time and datatime object

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!

PHP manipulates the datetime value of a mysql-select

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

PHP Datetime not working properly

I am retrieving the datetime data from mysql the retrieved data from a single row is.
2011-04-11 19:31:30
I wanted to reformat the datetime in d-m-Y H:i:s for that I am using date_format() the below code works just fine.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
However, I don't want to go object oriented way just for reformatting because I will be using the code within the foreach loop and that means I would have to initialize the DateTime class again and again.
I tried doing this the Procedural way using the following code.
$date = $users['registerDate'];
echo date_format($date, 'Y-m-d H:i:s');
the above code does not work for me and gives back the following error.
Warning: date_format() expects parameter 1 to be DateTime, string given in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/users/users.php on line 21
What could be possibly wrong?
The given solution works perfectly fine for the procedural way.
echo date('m-d-Y',strtotime($users['registerDate']));
However I would like to know which will be the best feasible solution the above procedural way or the OOP way.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
Considering I will be using the code within a foreach loop and it may loop for over a hundred times.
You do not need "date_format()":
echo date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
//results: 11-04-2011 19:31:30
You're trying to use a function/object that is part of the DateTime class without creating a reference to the DateTime class.
For procedural formatting take a look at date()
What you are seeking for is this:
date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
Have a look at the php-manual. However, using the methods you yourself proposed is pretty fine, since the DateTime-object maps to some functions written in C.
Also PHP Datetime is working properly, since date_format is just an alias of Date::format, which does exactly require what you don’t want to pass in (a DateTime-object).
Honestly, we’re talking about PHP...

Showing timezone specific dates in views with Zend_Date

I'm storing all my dates in ISO-format, so all of them look like this:
2010-08-17T12:47:59+00:00
Now, when my application starts, I register the timezone the current user resides in. In my case, this would be "Europe/Berlin":
date_default_timezone_set("Europe/Berlin");
However, when Zend_Date parses ISO dates, it overrides the default timezone set earlier and now has the UTC timezone.
But when I output this date in my view scripts I want it to show the date in the correct timezone.
Are there better solutions than writing a custom view helper just for this? (If this was the correct solution, shouldn't there already be a "DateViewHelper"?)
Not sure how this works with Zend_Date but given the fact that you use PHP >= 5.2 you can use the built-in DateTime class (which offers less functionality but is extremely faster):
$date = new DateTime('2010-08-17T12:47:59+00:00');
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo $date->format(DateTime::W3C);
EDIT
Just checked Zend_Date and it actually works the same here...
$date = new Zend_Date('2010-08-17T12:47:59+00:00', Zend_Date::ISO_8601);
$date->setTimezone(date_default_timezone_get());
echo $date->getIso();

Categories