I have a PHP function which inserts a date value from my SQL database into my PHP page here
$DateSql = "SELECT * FROM `ins_schedule` WHERE `active` = 1";
$DateResult = mysqli_query($connect, $DateSql);
$DateResultCheck = mysqli_num_rows($DateResult);
if ($DateResultCheck > 0){
while($row = mysqli_fetch_assoc($DateResult)){
echo "<p style=\"margin-left: 15px;\">{$row[('insider_date')]}</p>";
}
}
This method works fine for printing out the date like this
2020-01-31
But I want the date data to print out as "January 31, 2020". Would anyone know how to output the data like this?
There's a couple ways to do date formatting in PHP. Here I use the DateTime::format() method:
$DateSql = "SELECT * FROM `ins_schedule` WHERE `active` = 1";
$DateResult = mysqli_query($connect, $DateSql);
$DateResultCheck = mysqli_num_rows($DateResult);
if ( $DateResultCheck > 0 ) {
while ( $row = mysqli_fetch_assoc($DateResult) ) {
$date = new DateTime( $row['insider_date'] );
echo '<p style="margin-left: 15px;">' . $date->format('F j, Y') . '</p>';
}
}
Related
I have a small PHP page which takes data from MySQL and displays it via PHP in a monthly calendar. I'm having trouble arranging the data properly within an array to get the desired output.
First, I will describe what I would like to happen:
students come to classes on regular days of the week
they can also make or cancel reservations
the calendar also displays days when the school is not open
In order to display this data on the calendar, I use MySQL to output data from a variety of sources, and then input that into an array with PHP, which I sort by date and output.
My issue is, I would like to be able to handle more than one row of data per day, but because I am using the date as the key, I am limited on only displaying one result per day. If I use a loop to append the date with a counter in the key, I get overlapping results in situations where someone made a reservation and then cancelled that reservation on the same day.
As for my code...
First, I check to see if the student is registered in a weekly class, then input that class into the array.
$sql = "SELECT StudentDB.studentid, ClassDB.classID, ClassDB.class_level, ClassDB.class_title, ClassDB.time, ClassDB.teacher, StudentDB.first_name, StudentDB.last_name, StudentDB.payment_amount, ClassDB.day
FROM ClassDB
INNER JOIN RegDB ON ClassDB.classID = RegDB.classid
INNER JOIN StudentDB ON StudentDB.studentID = RegDB.studentid
WHERE StudentDB.studentid = '$studentid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { // DISPLAY REGULAR CLASS DATA
$dayofclass = $row['day'];
$class_level = $row['class_level'];
$class_title = $row["class_title"];
$day = $row["day"];
$class_time = $row["class_time"];
$time = $row["time"];
// check which dates match the days of the week and store in an array
for ($i=1;$i<=$n;$i++){
if ($i<10) {
$i = "0" . $i;
}
$day=date("l",strtotime($yearmonth.$i)); //find weekdays
if($day==$dayofclass){
$time = date("H:i",strtotime($row['time']));
$dates[]=$yearmonth.$i;
$datesdata[$yearmonth.$i] = "0";
$timedata[$yearmonth.$i] = $time;
$classiddate[$yearmonth.$i] = $row['classID'];
}
}
}
echo "</table>";
$conn->close();
}
After that, I check for specific reservations (cancelations, irregular reservations, waitlists) and input them into the array:
$lowerlimit = $yearmonth . "01";
$upperlimit = $yearmonth . "31";
$sql = "SELECT AttendanceDB.*, ClassDB.*
FROM StudentDB
INNER JOIN AttendanceDB ON StudentDB.studentid = AttendanceDB.studentid
INNER JOIN ClassDB ON AttendanceDB.classid = ClassDB.classID
WHERE StudentDB.studentid = '$studentid'
AND AttendanceDB.class_time >= '$lowerlimit'
AND AttendanceDB.class_time <= '$upperlimit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$loopcount = 0;
// store furikae data in the array
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["class_time"] );
$time = date("H:i",strtotime($row['time']));
$mysqldate = date( 'Y-m-d', $phpdate );
$loopcount++;
$mysqldate = $mysqldate . "+" . $loopcount;
// $loopcount++;
// $mysqldate = $mysqldate . "+" . $loopcount;
$previousdate = $mysqldate;
$previousfurikae = $row['furikae'];
if ($row["furikae"] == 3){
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "1";
$timedata[$mysqldate] = $time;
$classiddate[$mysqldate] = $row['classID'];
} elseif ($row["furikae"] == 8 OR $row["furikae"] == 7) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "3";
$timedata[$mysqldate] = $time;
} elseif ($row["furikae"] == 2) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "2";
$timedata[$mysqldate] = $time;
}
}
}
$conn->close();
Then finally I check the school calendar and input the days off into the array:
$sql = "SELECT *
FROM SchoolScheduleDB
WHERE date >= '$lowerlimit'
AND date <= '$upperlimit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// store furikae data in the array
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["date"] );
// $time = date("H:i",strtotime($row['time']));
// $mysqldate = date( 'Y-m-d', $phpdate ) . " " . $time;
$mysqldate = date( 'Y-m-d', $phpdate );
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "666";
}
}
$conn->close();
The way I intended it to work was that:
First the regular classes would be input
Then any reservations would overwrite the original plans
And finally the school calendar would overwrite everything
Currently, this functions as it should, but it is limited to displaying 1 result per day, but I would like to be able to display more than 1 result per day for students who come to multiple classes.
Thank you for your help. If I made any mistakes in my question or my question is unclear I will do my best to revise it.
You can make a Sub-Array for each date by using edged brackets:
$data[20180528][] = 'aa';
$data[20180528][] = 'bb';
$data[20180529][] = 'cc';
$data[20180529][] = 'dd';
$data[20180529][] = 'ee';
will give you an Array like this:
20180528 => aa
=> bb
20180529 => cc
=> dd
=> ee
I need to show (today) text if date is today. How do I do that!
I tried everything but cant find the right anwser that's why Im asking here.
Here is the piece of codeI have right now.
$mod_list.= '<ul class="upcoming-events"><li>
<div class="date">
<span><span class="day">'.date($dateformat,$datetime_start).'</span>
<span><span class="month">'.date($datemonth,$datetime_start).'</span>
<span><span class="year">'.date($dateyear,$datetime_start).'</span>
</div>';
I want to show it just behind the last </span>
It is for event calendar and I want to show the text TODAY on the exact day of the event.
Any Ideas?
if(date('Y-m-d',$datetime_start)==date('Y-m-d')){
//it's today! put code here
}else{
//it's not today
}
If you don't use the second parameter in the date() function, it uses the current time. So comparing their date to today is that simple.
Ok, here is the whole Code for what I'm using, it is actualy a droplet for showing the event calendar in my sidebar.
//:Show next #N events
//:
// Get_Concerts
global $database, $wb;
setlocale (LC_ALL, 'sl_SI.UTF-8'); //za vse kategorije
setlocale (LC_TIME, 'sl_SI.UTF-8'); //za datumske funkcije
// Show how many items, defaults to 10?
if ( !isset($max) ){ $max = 10; };
// year and month and section defaults
if(!isset($year)) {$year = date('Y', time()); }
if(!isset($month)) {$month = date('n', time()); }
if(!isset($section_id)) {$section_id = 0 ; }
// Set dateformat to suit your needs, add timeformat if needed
$dateformat = 'd'; // Standard php date formats
$datemonth = 'M'; // Standard php date formats
$dateyear = 'Y'; // Standard php date formats
// Fetch base page link, if event_id = set
$extrasql = '';
$page_id = 0;
$page_link ='';
if ($section_id<>0) {
$extrasql = " section_id = '".$section_id."' AND ";
$sql = "SELECT page_id FROM ".TABLE_PREFIX."sections WHERE section_id = '".$section_id."'";
$result = $database->query($sql);
if ( $result->numRows() > 0 ) {
while( $row = $result->fetchRow() ) {
$page_id = $row['page_id'];
}
}
if ($page_id <> 0) {
$sql = "SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '".$page_id."'";
$result = $database->query($sql);
if ( $result->numRows() > 0 ) {
while( $row = $result->fetchRow() ) {
$page_link = page_link($row['link']);
}
}
}
}
// Set start- and end date for query
// $datestart = "$year-$month-1"; ORIGINAL = show all events in this month
$datestart = date("Y-m-d"); // ALTERNATIVE = show all events in this month, starting today
$dateend = "$year-$month-".cal_days_in_month(CAL_GREGORIAN, $month,$year);
$mod_list = "";
// Fetch the items
$sql = "SELECT DAY(date_start) AS day, id, custom1, date_start, time_start, date_end, time_end, name FROM ".TABLE_PREFIX."mod_procalendar_actions WHERE ".$extrasql." date_start >='$datestart' AND public_stat = 0 ORDER BY date_start,time_start LIMIT 0, ".$max." ";
$mod_query = $database->query($sql);
while ( $row =& $mod_query->fetchRow()){
// Build url like : pages/kalendar.php?id=2&detail=1
$page_url = $page_link.'?id='.$row['id'].'&detail=1';
$ds = $row['date_start']." ".substr($row['time_start'],0,5);
$de = $row['date_end']." ".substr($row['time_end'],0,5);
$datetime_start = mktime(substr($ds,11,2),substr($ds,14,2),0,substr($ds,5,2),substr($ds,8,2),substr($ds,0,4));
$datetime_end = mktime(substr($de,11,2),substr($de,14,2),0,substr($de,5,2),substr($de,8,2),substr($de,0,4));
if ($row['time_start'] !== $printTime) {
$printTime = $row['time_start'];
$mod_list.= '<ul class="upcoming-events"><li>
<div class="date"><span><span class="day">'.date($dateformat,$datetime_start).'</span><span><span class="month">'.date($datemonth,$datetime_start).'</span><span><span class="year">'.date($dateyear,$datetime_start).'</span>
</div>';
$mod_list.= '<div class="event-content"><h6>'.$row["name"].'</h6>
<ul class="event-meta"><li><i class="fa fa-clock-o"> </i>'.substr($printTime,0,5).'<sup>h</sup></li>
<!-- <li><i class="fa fa-info-circle"> </i>'.$row["custom1"].'</li> --></ul></div></li></ul>';
}
$mod_list .= "<hr>";
}
$mod_list .= 'Napovednik<br></br>';
return $mod_list;
Here is the link to the site, LINK you'll notice a TEST event which is created for today, I want a little TODAY text to be shown right beside the HOUR on the right.
I hope this helps more, Thank you
R.
I can't figure it out! I've created an array of months in "Slovenian language" and now, I would want to display my month's name instead of the number. Instead of working, it writes out - 32014vEurope/Berlin11bEurope/BerlinWed and some more weird stuff, it should obviously print out November in my case. I would like to solve this problem with arrays, but It just wouldn0t convert the number of 'n' to the requested month.
function kliknjena($link, $mojster)
{
$meseci[1] = "Januar";
$meseci[2] = "Februar";
$meseci[3] = "Marec";
$meseci[4] = "April";
$meseci[5] = "Maj";
$meseci[6] = "Junij";
$meseci[7] = "Julij";
$meseci[8] = "Avgust";
$meseci[9] = "September";
$meseci[10] = "Oktober";
$meseci[11] = "November";
$meseci[12] = "December";
$sql = "SELECT naslov, podnaslov, vsebina, ustvarjeno, slug FROM novica
where slug = '$mojster'
limit 1";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<h1>".$row["naslov"]."</h1>";
$timestamp = strtotime($row["ustvarjeno"]);
$m = date("n", $timestamp);
echo date("d. $meseci[$m]; Y, H:i:s", $timestamp);
echo "<p>".$row["podnaslov"]."</p>"."<br>";
echo "<p>".$row["vsebina"]."</p>"."<br>";
}
}
else
{
echo "0 results";
}
}
Use:
echo date('d. ', $timestamp) . $meseci[$m] . date('; Y, H:i:s', $timestamp);
What's happening is that the month name is being substituted into the date() argument, and then all the letters in the month are being treated as formatting characters, so they get replaced with the corresponding fields from the date and time.
I am getting time from my database, when I echo it out it works, but it doesn't work in the while loop.
However, the other variables work...
Code:
$resultevent = mysql_query('SELECT venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');
$rowevent = mysql_fetch_assoc($resultevent);
$date = new DateTime($originaldate);
$newdate = $date->format('m/d/Y');
$startTime2 = $rowevent['startTime'];
$endTime2 = $rowevent['endTime'];
$venue = $rowevent['venue'];
echo $startTime2; <============== WORKS HERE
while ($row = mysql_fetch_assoc($result)) { //the code for these queries is not shown
echo 'getting here'; <======= WORKS
echo $venue; <======= WORKS
echo $startTime2; <======= DOESNT WORK
}
Your query is returning your time columns with an alias. To read them from your row you should use the alias as an index:
$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];
1.In your loop where does $results came from??
2.You set startTime as startTime2 in your query same as endTime to endTime2.
and obviously it will not work even your echo $venue.. because you fetch nothing.
it should be like this code:
$date = new DateTime($originaldate);
$resultevent = mysql_query('SELECT venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');
while ($rowevent = mysql_fetch_assoc($resultevent)) { //now you fetch your query in $resultevent
$newdate = $date->format('m/d/Y');
$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];
$venue = $rowevent['venue'];
echo 'getting here';
echo $venue;
echo $startTime2;
}
<?php
$con=mysqli_connect("localhost","*********","***********","loadtracker");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Date FROM pallettracker GROUP BY Date");
$DateList = mysqli_fetch_array($result);
$ListOfMonths=array('Dec','Nov','Oct','Sep','Aug','Jul','Jun','May','Apr','Mar','Feb','Jan');$ListOfDays = array('31','30','29','28','27','26','25','24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6','5','4','3','2','1');
foreach ($ListOfDays as $value) {
foreach ($ListOfMonths as $value2) {
$Yaer = array("'$value2' + '$value' + ',2013'");
foreach ($Yaer as $yaerdate) {
foreach ($DateList as $Dates) {
if ($yaerdate == $Dates)
{
echo $yaerdate;
echo '/';
}
}
}
}
}
?>
trying to order a array of dates foratted as Jan28,2013 and Decending. And then display then with a / in-between them. For an Android App
You can have your SQL query order them for you. This assuming that the column is of the Date format. If it is not check out this: SQL ORDER BY date problem.
After ordering in the query, you just need to convert it to a time then format the date as desired. This might give you what you are looking for:
<?
$datetime = strtotime($row->createdate);
$mysqldate = date("m/d/y g:i A", $datetime);
$result = mysqli_query($con,"SELECT Date FROM pallettracker GROUP BY Date");
foreach($result as $date){
$datetime = strtotime($result);
$date = date("M d, y", $datetime);
echo $date . '/';
}
?>
I don't get it, why you are using GROUP BY when you don't use a grouping function.
The better way here is DISTINCT
I would update your code in this way:
$result = mysqli_query($con,"SELECT DISTINCT `Date` FROM `pallettracker` ORDER BY `Date` DESC");
while($date = mysqli_fetch_assoc($result)
{
echo date('',strtotime($date['Date'])).'<br />';
}
It is important that you "Date" col in mysql is in the format Date or Datetime