date() assistance...

  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 2:52 am

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:
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 5th, 2009, 2:52 am

  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 4:53 am

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
  • Joined: Apr 09, 2008
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England
  • Status: Offline

Post June 5th, 2009, 6:52 am

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
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 7:00 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post June 5th, 2009, 7:03 am

Try something like:

Code: [ Select ]
$newdate = strtotime ( '-1 month' , strtotime ( $date ) ) ;
I'd love to change the world, but they won't give me the source code.
  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 7:14 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post June 5th, 2009, 7:18 am

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.
I'd love to change the world, but they won't give me the source code.
  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 7:25 am

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
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 7:30 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post June 5th, 2009, 7:34 am

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.
I'd love to change the world, but they won't give me the source code.
  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 7:49 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post June 5th, 2009, 8:11 am

EDIT: hold tight, I'm fixing this...
I'd love to change the world, but they won't give me the source code.
  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 8:20 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post June 5th, 2009, 8:28 am

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.
I'd love to change the world, but they won't give me the source code.
  • tiffix
  • Student
  • Student
  • User avatar
  • Joined: Jun 03, 2009
  • Posts: 65
  • Loc: kenya
  • Status: Offline

Post June 5th, 2009, 8:29 am

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?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 5th, 2009, 8:29 am

Post Information

  • Total Posts in this topic: 21 posts
  • Users browsing this forum: No registered users and 80 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.