For the code below, $count++ prints out rows of numbers ascending from 1 on up.
How could I replace it with monthly intervals starting with September 1, 2010 6 am eastern? (I. e. row one has September 1, 2010, row 2 has October 1, 2010, etc.?)
I would like the dates to be in a format that would allow me to compare to timestamps from MySQL
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table>";
while ($row = mysql_fetch_array($result)) {
echo '<tr >';
echo '<td >'.$count++.'</td>';
I’m a fan of the
strtotime()function. It takes a readable string and converts it into a Unix timestamp, and it handles relative time really well as well.First, create a variable with your initial date:
Each month in the loop is created by simply adding one month to the variable:
You can use
date()to format the timestamp to output it in your list:You can see the full range of formats here.
Alternately, if you’re using PHP 5.3, you can take full advantage of the DateTime object (introduced in 5.2, but things like
DateTime::add()didn’t exist until 5.3).As far as compatibility with MySQL, you can either use the MySQL function
UNIX_TIMESTAMPto return a Unix timestamp from your query, or run the MySQL timestamp throughstrtotime()as well to convert it to a Unix timestamp.