Getting Month and Day number from database field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Getting Month and Day number from database field

Post by marnieg »

I have a table with a DATE type field. I want to first place the field into a variable in my php code and do some manipulations on it before echoing the Month and Day number of the field.

First I get the field from the database and place it into a field

$csedate = $rows->course_stdte;

Then I want to get the month and day number from this field, what function do I use. I have tried Date, Date_Format, Substring, but I think these are all MySQL functions and not php functions. Then I want to echo the date as mm/dd. Then I want to add 7 days to it and echo the date again as mm/dd and repeat this process. I think I know how to use the while loop for this, but getting the month and day number from the database field is my problem.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Getting Month and Day number from database field

Post by marnieg »

Here is my code if this helps as it is now.

Code: Select all

   $csenextdte = $csestart;
 							   while ($cols <= $csetotwks )
 							      {
 							      if ($cols == 0) {
 							         $csestmth = date('m',$csestart);
 							         $csestday = date('d',$csestart);
 							         }
 							      else
 							      {
 							         $csenextdte = $csenextdte + 7;
 							         $csestmth =  date('m',$csestart);
 							         $csestday =  date('d',$csestart);
 							         
 							      }
 							      	 $csedate = $csestmth;
 							         $csedate .= "/";
 							         $csedate .= $csestday;
 							         echo "<td><b> $csedate </b></td>";
 	                                 
 							      $cols++;
 							      }
Last edited by Benjamin on Tue May 18, 2010 4:27 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Getting Month and Day number from database field

Post by mikosiko »

maybe this can help you to tackle your problem in a different way

http://www.php.net/manual/en/function.date-add.php
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Getting Month and Day number from database field

Post by pickle »

You can also use the MySQL function UNIX_TIMESTAMP() to convert from a MySQL date format, to a UNIX timestamp. You can then use date() and strtotime() to output the date & increment 7 days.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Getting Month and Day number from database field

Post by marnieg »

My problem is still how to add 7 days to my date field.

I have a field in my table that is type DATE. When I display the value of the is field it is 2010-02-02. So now I want to add 7 days to it and then load it into an array for later echoing.

So the question is how do I add the 7 days to the field. I have tried just a straight +

$csenextdte = $csenextdte + 7;

Also date_add and DATEInterval, but I must not have the syntax correctly so in pseudo code this is what I want

Take the original date and add 7
store it in an array
loop through this process certain number of times

Here is my existing code
the variable $csestart contains the value from my database record
$cols = 0;
$csedatenew = strtotime($csestart);
$csenextdte = $csestart;
while ($cols <= $csetotwks)
{
if ($cols == 0) {
$csestmth = date('m',$csedatenew);
$csestday = date('d',$csedatenew);
$csedate = $csestmth;
$csedate .= "/";
$csedate .= $csestday;
$array[] = $csedate; - loading array with first value - this works fine
}
else
{

$csenextdte = $csenextdte + 7; - Here is the code in question. How do I add 7 to the date
//$csestmth = date('m',$csenextdate);
//$csestday = date('d',$csenextdate);
//$csedate = $csestmth;
//$csedate .= "/";
//$csedate .= $csestday;
array_push($array,$csenextdte);
}
$cols++; - increment the number and load the next date
}
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Getting Month and Day number from database field

Post by mikosiko »

did you use pickle's suggestions strtotime()/date()? (did you tried/test?) you probably will notice that strtotime() will convert you date in a timestamp.... add 7 days is just a matter to add (86400 * 7) to that timestamp and after that you can format that timestamp in the way you want using date().

try
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Getting Month and Day number from database field

Post by marnieg »

$csenextdte = strtotime($csestart); - I use strtotime on DATE field csestart value is 2010-02-02

$csendte = $csenextdte + 7; - then I add 7 days to it, but it comes up with the same date as the original date.
$csestmth = date('m',$csendte);
$csestday = date('d',$csendte);
$csedate = $csestmth . "/" . $csestday;
echo $csedate ;
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Getting Month and Day number from database field

Post by marnieg »

Maybe the problem is the format of my date field in the database, but I thought converting it using the strttotime would put it into proper format then add 7. Maybe it is adding 7 to the year and not the day.
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: Getting Month and Day number from database field

Post by marnieg »

Found another function on another site that worked.


function nextWeeksDay($date_begin,$nbrweek)
{
$nextweek=array();
for($i = 1; $i <= $nbrweek; $i++) {
$nextweek[$i]=date('m d', strtotime('+'.$i.' week',$date_begin));
}
return $nextweek;
}
/// end function

then code executed
$nbrweek = $csetotwks;
$result=nextWeeksDay($csenextdte,$nbrweek);
for ($i=1; $i <=$nbrweek; $i++) {
array_push($array,$result[$i]);
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Getting Month and Day number from database field

Post by pickle »

Dude - seriously. Read the documentation. strtotime() will give you a date in seconds. Adding 7 will add 7 seconds. Read the documentation for strtotime().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply