/    Sign up×
Community /Pin to ProfileBookmark

PHP and MySQL Dates and Splitting

Hey,

Im creating a calendar, that would show months in a year and inputting the booking in that date slot.

On the booking form the dates are entered in to the MySQL Database as: year-month-day (2010-11-17)

Below gets the month/year from user input, works fine.

[code=php]
function getmicrotime(){
list($usec, $sec) = explode(” “,microtime());
return ((float)$usec + (float)$sec);
}

$time_start = getmicrotime();

if (!isset($_GET[‘year’])){
$_GET[‘year’] = date(“Y”);
}
if (!isset($_GET[‘month’])){
$_GET[‘month’] = date(“n”)+1;
}

$month = addslashes($_GET[‘month’] – 1);
$year = addslashes($_GET[‘year’]);

[/code]

Below here gets the date from the bookings table, and splits up the date, using split() i got this from the PHP manual.

[code=php]

$bookQueryA = mysql_query(“SELECT * FROM bookings”);
while ($row = mysql_fetch_array($bookQueryA))
{
$date = $row[‘Booking_Date’];
list($b_year, $b_month, $b_day) = split(‘[-]’, $date);
}

[/code]

Now heres where i get stuck, how do i use the WHERE to only grab the bookings thats set by the $month and $year variables.

[code=php]
$bookQueryB = mysql_query (“SELECT * FROM bookings WHERE booking_month=’$month’ AND booking_year=’$year’ ORDER BY Booking_STime”);
while ($row2 = mysql_fetch_array($bookQueryB))
{
$day = $b_day;
$booking_id = $row2[‘id’];
$booking[$day][] = $row2[‘id’];
$booking_info[$booking_id][‘0’] = substr($row2[‘Booking_Name’], 0, 8);;
$booking_info[$booking_id][‘1’] = $row2[‘Booking_STime’];
}
[/code]

Maybe i have the code an little muddled up, but any help would be highly grateful.

Thanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@LiL_aaronauthorNov 17.2010 — Hmm after reading back... maybe something like this:


[code=php]

$bookQueryA = mysql_query("SELECT * FROM bookings");

while ($row = mysql_fetch_array($bookQueryA))
{
$date = $row['Booking_Date'];
list($b_year, $b_month, $b_day) = split('[-]', $date);

if (($b_year) && ($b_month) == ($year) && ($month)) {
$day = $b_day;
$booking_id = $row2['id'];
$booking[$day][] = $row2['id'];
$booking_info[$booking_id]['0'] = substr($row2['Booking_Name'], 0, 8);;
$booking_info[$booking_id]['1'] = $row2['Booking_STime'];
}

}

[/code]


Would this work?
Copy linkTweet thisAlerts:
@LiL_aaronauthorNov 17.2010 — Hmm again some more modification...

[code=php]

$bookQueryA = mysql_query("SELECT * FROM bookings");

while ($row = mysql_fetch_array($bookQueryA))
{
$date = $row['Booking_Date'];
list($b_year, $b_month, $b_day) = split('[-]', $date);

echo $b_month;
echo "---".$b_year;

if($b_year == $year){
} elseif($b_month == $month){
$day = $b_day;
$booking_id = $row['id'];
$booking[$day][] = $row['id'];
$booking_info[$booking_id]['0'] = substr($row['Booking_Name'], 0, 8);;
$booking_info[$booking_id]['1'] = $row['Booking_STime'];
}

}

[/code]


I did some debugging...

[code=php]
echo $b_month;
echo "---".$b_year;
[/code]


which outputs.... 11---201008---2010

i then was thinking ... what the hell.... then realised thats 2 dates of course !! Duh

11---2010

08---2010

Now heres the real question how do i split the array :rolleyes:
Copy linkTweet thisAlerts:
@LiL_aaronauthorNov 17.2010 — More changes... but still unsuccessful

[code=php]
$bookQueryA = mysql_query("SELECT * FROM bookings");

while ($row = mysql_fetch_array($bookQueryA))
{
$date = $row['Booking_Date'];
list($b_year, $b_month, $b_day) = split('[-]', $date);
$b_month++;

//echo $b_month;
//echo "---".$b_year;
//$b_month = explode(" ", $b_month);

$arr = array($b_month);
reset($arr);

foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />n";
}

if($b_year == $year){
} elseif($b_month == $month){
$day = $b_day;
$booking_id = $row['id'];
$booking[$day][] = $row['id'];
$booking_info[$booking_id]['0'] = substr($row['Booking_Name'], 0, 8);;
$booking_info[$booking_id]['1'] = $row['Booking_STime'];
}

}
[/code]


Output:


Key: 0; Value: 12

Key: 0; Value: 9
Copy linkTweet thisAlerts:
@criterion9Nov 17.2010 — Wouldn't it be easier to just translate the date?

In PHP (if the column is not a date type)
[code=php]
$stuff = '2010-11-17';
$month = date('m', strtotime($stuff));
[/code]



In SQL (better choice but only works if the column is a date type)
<i>
</i>SELECT MONTH(columnThatIsADateType) as YourMonthOnly FROM table;
Copy linkTweet thisAlerts:
@LiL_aaronauthorNov 18.2010 — Yay got it working... thanks for the idea criterion...

[code=php]

$bookQueryB = mysql_query("SELECT DAY(Booking_Date) as 'Day', id, Booking_Name, Booking_STime FROM bookings WHERE MONTH(Booking_Date) = '$month' AND YEAR(Booking_Date) = '$year'");

while ($row = mysql_fetch_array($bookQueryB))
{
$day = $row['Day'];
$booking_id = $row['id'];
$booking[$day][] = $row['id'];
$booking_info[$booking_id]['0'] = substr($row['Booking_Name'], 0, 8);;
$booking_info[$booking_id]['1'] = $row['Booking_STime'];
}

[/code]
×

Success!

Help @LiL_aaron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...