Jump to content

[SOLVED] learning about new DateTime(); class in PHP 5.2.x got a question


severndigital

Recommended Posts

Ok so i am learning about classes as a whole .. but specifically working with the DateTime() class that is built into PHP 5.2.x

 

I have a pretty noob questions, but can't seem to find it anywhere.

 

suppose it do this

<?php

$date = new DateTime();
echo 'Today is ' . $date->format('l, F jS, y');
//then modify the date to show the date in say ... 6 mos.
$date->modify('+6 months');
echo 'In 6 Mos. it will be ' . $date->format('l, F jS, Y);

 

how do i get rid of the modifer to get back to the original date??

if i continue the code above with this

 

$date->modify('-1 month;);
echo 'Last Month was ' . $date->format('l, F jS, Y);

 

i get whatever the last modifer was minus 1 (5 months ahead).

 

if there an easy way to clear the modifer from the original class, or do i need to do reverse math??

 

Thanks,

C

 

 

 

 

the only problem with doing another modify is that is will change the original time.

 

since it happens later in the script.

 

with this small of a script it happens very fast you need to use the B command in date to see it.

 

take this code

$date = new DateTime();
echo 'today is ' . $date->format('l, F jS, Y H:m:i:s.B');
echo '<br/>In 6 months it will be<br/>';
$date->modify('+6 months');
echo '' . $date->format('l, F jS, Y');
echo '<br/>One Jan. 2nd it will be<br/>';
$date->setDate(2009,1,2);
echo '' . $date->format('l, F jS, Y');
echo '<br/>Last Month Was<br/>';
$date->modify('-1 months');
echo '' . $date->format('l, F jS, Y');
echo '<BR/>';
$date->modify(time());
echo '2nd today is ' . $date->format('l, F jS, Y H:m:i:s.B');

 

it doesn't seem like a lot of script but resetting using the time() command throws it off but a few Nth's of a second.

in a longer script this could be a problem if it takes 1 or 2 seconds to load.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.