date() assistance...

  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

i capture my month entries in this format Jun, 2009 which i then assign to variable $month. How do i use php to reduce this month by 1 month so that variable $motnh becomes May, 2009 when am reading it back to php from mysql :wink:
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

how do i make use of

Code: [ Select ]
date("M, Y", strtotime("-1 month"));


and yet my month is stored by $month in this format Jun, 2009 :idea:
  • tastysite
  • Proficient
  • Proficient
  • User avatar
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England

Post 3+ Months Ago

all you do its this
Code: [ Select ]
<?php print date('m')-1 ?>

this it! Then add the year at the end and you are done save that as $month and you are all set.
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

hello Tastysite,
i've already stored the month to mysql as Jun, 2009 this is a string in a variable char. field. now am reading this string back from mysql and as you know it's one long string as "Jun, 2009" for example. now how do we reduce or add a month or two to this. thanks.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Try something like:

Code: [ Select ]
$newdate = strtotime ( '-1 month' , strtotime ( $date ) ) ;
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

Hi Master UPsGuy,
thanks i managed to solve the other issue. back to today? i guess $date is the $month i was talking about? let me give it a try... will keep you posted
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Yes, sorry, $month == $date. I hope php will convert your string properly O_o. If you have any issues it will most likely be around the fact that you're converting "Jun, 2009" to a date. strtotime is good, but I don't know how good.
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

wow, it worked like a charm but i had to converet the big int. timestamp back to user friendly value by using

Code: [ Select ]
     $month="Jun, 2009";
        
     $newdate = strtotime ( '-1 months' , strtotime ( $month ) ) ;
        
     echo date("M, Y", $newdate) ;
  1.      $month="Jun, 2009";
  2.         
  3.      $newdate = strtotime ( '-1 months' , strtotime ( $month ) ) ;
  4.         
  5.      echo date("M, Y", $newdate) ;


and i got a nice May, 2009

THANKS
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

With this, i have the ability to add and subtract months as i wish. hope it will be consitent considering am converting a string to a date. anyway, all i well now. big shout out to all members
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Since you're just subtracting the Month by itself, you should be fine. The only catch to adding/subtracting months involves full dates and the end of a long month, for instance, 03/31/2009 - 1 month will do weird things since Feb. only has 28 days, but that shouldn't be an issue for you.
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

is there a way to find out the immediate last wednesday from a specified date by a user. if for exampl the user picks Jun 05, 2009. then the system should automatically compute that from 5th jun, the immediate wednesday was Jun 03, 2009 and if he pick May 18, 2009 then immediate last wednesday was May 13, 2009 kindly let me know?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

EDIT: hold tight, I'm fixing this...
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

let me test it, if it works then this is a great break through for my project... is see the concept you have :D
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

OK, this time it's been tested: ;)

Code: [ Select ]
 
<?php
 
$myDate = date("m/d/Y", strtotime("June 8, 2009"));
 
do {
     $myDate = strtotime('-1 day' ,strtotime($myDate));
     $myDate = date('Y-m-d', $myDate );
}while(date("w", strtotime($myDate)) != '3');
 
echo date("m/d/Y", strtotime($myDate))."</br>";
echo date("w", strtotime($myDate))."</br>";
?>

//output:
//06/03/2009
//3
 
  1.  
  2. <?php
  3.  
  4. $myDate = date("m/d/Y", strtotime("June 8, 2009"));
  5.  
  6. do {
  7.      $myDate = strtotime('-1 day' ,strtotime($myDate));
  8.      $myDate = date('Y-m-d', $myDate );
  9. }while(date("w", strtotime($myDate)) != '3');
  10.  
  11. echo date("m/d/Y", strtotime($myDate))."</br>";
  12. echo date("w", strtotime($myDate))."</br>";
  13. ?>
  14. //output:
  15. //06/03/2009
  16. //3
  17.  


Of course, the do-while is what you'll mostly be interested in, but this shows how to set it up and also pull the result - things I had to do to test it.
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

am afraid i get a wired date of
Quote:
Jun 04, 2009Dec 31, 1969
when i do this:

Code: [ Select ]
$myDate = "June 5, 2009";
        do {
            $myDate = strtotime('-1 day', strtotime($myDate));
            echo date("M d, Y", $myDate);         
                        
        }while(date("w", $myDate) != '3');
  1. $myDate = "June 5, 2009";
  2.         do {
  3.             $myDate = strtotime('-1 day', strtotime($myDate));
  4.             echo date("M d, Y", $myDate);         
  5.                         
  6.         }while(date("w", $myDate) != '3');


any ideas?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Yep, check the post I just made - I had an error in my code, but the newer post has been tested.
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

sorry i posted the above before refreshing my browser, otherwise can you stick to this date format M d, Y please.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I think you can figure out how to plug in your own format. I don't mind assisting, but if you're just looking for someone to write the code for you and you're not willing to learn it, that I can't help you anymore.
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

Assuming we have these dates in mysql already in this formt then we ask a user to pick a date for his current savings and then we show the current plus last wednesday's savings. thats what am tryin...
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

sure i can do the code convertion but i thot the example was strickly for the format you gave! if it's flexible then let me give it a shot. thanks a lot anyway , Master
  • tiffix
  • Student
  • Student
  • User avatar
  • Posts: 65
  • Loc: kenya

Post 3+ Months Ago

Men You Rock UPsGuy. It Worked well as i Wanted. i really appreciate you support

here's my code courtesy of you:

Code: [ Select ]
$myDate = date("M d, Y", strtotime("May 18, 2009"));
 
    do {
         $myDate = strtotime('-1 day' ,strtotime($myDate));
         $myDate = date('M d, Y', $myDate );
    }while(date("w", strtotime($myDate)) != '3');
     
    echo date("M d, Y", strtotime($myDate))."</br>";
    //echo date("w", strtotime($myDate))."</br>";
 
  1. $myDate = date("M d, Y", strtotime("May 18, 2009"));
  2.  
  3.     do {
  4.          $myDate = strtotime('-1 day' ,strtotime($myDate));
  5.          $myDate = date('M d, Y', $myDate );
  6.     }while(date("w", strtotime($myDate)) != '3');
  7.      
  8.     echo date("M d, Y", strtotime($myDate))."</br>";
  9.     //echo date("w", strtotime($myDate))."</br>";
  10.  



Thanks :D

Post Information

  • Total Posts in this topic: 21 posts
  • Users browsing this forum: No registered users and 84 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.