<?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
Related
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>';
}
}
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 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 have a DATE field on my MySQL table. Let's say the value is 2015-05-05.
I would like to check if the current time is before or after that.
This is my code:
foreach ($row as $row)
{
$ExpDate = strtotime($row['exp_date']);
$Today = strtotime(date("Y-m-d"));
echo $Today . ' - ' . $ExpDate;
if ($Today > $ExpDate)
{
exit('<M>LicenseExpired<M>');
}
}
The problem is that it's not working. The value of exp without strtotime is 2015-05-05. It I add the strtotime, the value becomes an empty string.
How am I able to solve this problem or what would be a good way to compare dates in PHP?
When comparing dates in MySQL there is no reason to take the extra step and use string_to_time(). The below example should work just fine. The format of MySQL DATE is designed in such a way that comparisons of this nature work naturally without any extra steps needed.
foreach ($row as $row)
{
$ExpDate = $row['exp_date'];
$Today = date("Y-m-d");
echo $Today . ' - ' . $ExpDate;
if ($Today > $ExpDate)
{
exit('<M>LicenseExpired<M>');
}
}
try this
foreach ($row as $row)
{
$ExpDate = new DateTime($row['exp_date']);
$Today = new DateTime(date("Y-m-d"));
$interval = $ExpDate->diff($Today);
//echo $interval->format('%R%a days'); <--- to diffenernce by days.
if ($interval > 0)
{
exit('<M>LicenseExpired<M>');
}
}
I'm trying to populate a form SELECT with OPTIONS from a table in my database. It should display each year once.
eg. If the date fields in the database are 02-09-2010, 10-14-2010, 08-09-2011 :
The dropdown should show:
2010
2011
Instead, it shows:
2010
2010
2011
My code:
$result = mysql_query("SELECT date FROM user_history");
$yearoptions = "";
while($row = mysql_fetch_array($result)) {
$date = $row['date'];
$yeararray = explode("-", $date);
$year = array_unique($yeararray);
$yearoptions .= '<option value="' . $year[2] . '">'
. $year[2] . '</option>';
Can anyone see what I'm doing wrong and offer a suggestion?
$result = mysql_query("SELECT distinct year(date) as years FROM user_history ORDER BY 1 ASC");
<?php while($row = mysql_fetch_array($result)) { ?>
<option value="<?php echo $row['years'];"><?php echo $row['years']; ?></option>
<?php endwhile; ?>
SELECT DISTINCT YEAR(date) FROM user_history, maybe with ORDER BY date, would make the PHP part much simpler...
Try: SELECT DISTINCT YEAR(date) FROM user_history
First thing, may I suggest that you use date_parse on the $row['date'], something like this.
$date = date_parse($row['date']);
$year = $date['year'];
The reason years are showing up twice is that you have no check for redundancy. I would recommend adding each year to an array and using in_array($year, $array) to check if you already have it added.
Try:
SELECT DISTINCT YEAR(date) as date FROM user_history ORDER BY date asc
You dont need the code that you had to extract the year component this way...
Your full code would be:
$result = mysql_query("SELECT DISTINCT YEAR(date) as date FROM user_history ORDER BY date asc");
$yearoptions = "";
while($row = mysql_fetch_array($result)) {
$date = $row['date'];
$yearoptions .= '<option value="' . $date . '">'
. $date . '</option>';
Edite You may also find the following useful when working with dates / times in the future http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
You aren't checking for duplicates in the year before outputting it. That's your base problem.
You should store the possible years as an array first, then populate the options based on that array. That way, you won't have to worry about duplicating. If you have 11.26.2009, 09.01.2010, 12.15.2010, 06.05.2011, iterate through all the results for an array similar to $yearOptions = array('2009', '2010', '2011');
The other benefit here is if there's an error with the way the dates are displaying, you can simply var_dump($yearOptions); to see what's showing up in there.
Finally, I'd suggest using strtotime() instead of explode(), simply because it can accept a potentially non-standard date format and still return a proper UNIX timestamp. Then you can manipulate it using date(). Extra mini-bonus: if you ever want to switch to '09 rather than 2009, it's a simple switch.
$result = mysql_query("SELECT date FROM user_history");
$yearOptions = array();
while($row = mysql_fetch_array($result)) {
$date = $row['date'];
// convert the date to a UNIX timestamp
$date = strtotime($date);
// retrieve just the Y (xxxx) from the timestamp
$year = date('Y', $date)
// if the year doesn't already exist in the array, add it.
if(!in_array($year, $yearOptions)) { $yearOptions[] = $year; }
}
foreach($yearsOptions as $year) {
echo('<option value="' . $year . '">' . $year . '</option>');
}
Hope it works.
As mentioned above, the easiest way would be
SELECT DISTINCT YEAR(date) FROM user_history ORDER BY date
but if the date is not saved in database a regular DATE db type, you cannot apply the year function and then you need to process manually.
$result = mysql_query("SELECT date FROM user_history");
$yearoptions = "";
$yearArray = array();
while($row = mysql_fetch_array($result)) {
$date = $row['date'];
$yeararray = explode("-", $date);
array_push($yearArray, $year[2]);
}
$yearArray = array_unique($yearArray); // Keep only distinct elements
$yearArray = array_sort($yearArray); // To have years in asc order
for(int i=0; i<sizeof($yearArray); i++) {
$yearoptions .= '<option value="' . $yearArray[i] . '">'. $yearArray[i] . '</option>';
}