severndigital Posted October 1, 2008 Share Posted October 1, 2008 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 Link to comment https://forums.phpfreaks.com/topic/126637-solved-learning-about-new-datetime-class-in-php-52x-got-a-question/ Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 I haven't used this class before, but I'm thinking something like: $date->modify(time()); Link to comment https://forums.phpfreaks.com/topic/126637-solved-learning-about-new-datetime-class-in-php-52x-got-a-question/#findComment-654866 Share on other sites More sharing options...
severndigital Posted October 1, 2008 Author Share Posted October 1, 2008 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. Link to comment https://forums.phpfreaks.com/topic/126637-solved-learning-about-new-datetime-class-in-php-52x-got-a-question/#findComment-654870 Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 What if you assigned a variable to time() at the beginning of the script, then run the modify function with that variable? Link to comment https://forums.phpfreaks.com/topic/126637-solved-learning-about-new-datetime-class-in-php-52x-got-a-question/#findComment-654873 Share on other sites More sharing options...
severndigital Posted October 1, 2008 Author Share Posted October 1, 2008 interesting .. and works .. for now I'll consider it done. weird though that there isn't a way to strip a class modifier at all. maybe in php 6 Thanks Link to comment https://forums.phpfreaks.com/topic/126637-solved-learning-about-new-datetime-class-in-php-52x-got-a-question/#findComment-654879 Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 There may be a way, but the documentation on that class isn't very thick. Link to comment https://forums.phpfreaks.com/topic/126637-solved-learning-about-new-datetime-class-in-php-52x-got-a-question/#findComment-654880 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.