Sorry my English a little,
My date column is 1375801584 valuable epoch format.
I wanna only select now and next time records, i want hide history times record.
eg: 19:45 and later.
<?php
$sql = mysql_query("select tarih from table where tarih < '$date' order by tarih ASC");
while($r = mysql_fetch_assoc($sql)){
?>
<tr><td><?php echo date("Y-m-d", $r['tarih']); ?></td>
<td><?php echo date("H:i", $r['tarih']); ?></td></tr>
<?php
}
?>
You close. Your current query is selecting older records (<). Try using >=:
$sql = mysql_query("select date from table where date >= '$date;' order by date ASC");
Related
These are the rows:
This is my picture of whats in the rows:For a project I have to make a calendar. The day and month have to be in chronological order, like name: (DD-MM-YYYY). Now I have already made the table in html and php but I don't know how to sort it.
<?php
$connection = new mysqli('localhost','root','','calendar');
$sql = "SELECT * FROM birthdays";
$result = $connection->query($sql);
$birthdayList = $result->fetch_all(MYSQLI_ASSOC);
?>
<!doctype html>
<html>
<head>
<title>Verjaardagskalender</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>
<table>
<?php
foreach($birthdayList as $birthdays){
?>
<tr>
<td><?php echo $birthdays['person'];?></td>
<td><?php echo $birthdays['day'];?></td>
<td><?php echo $birthdays['month'];?></td>
<td><?php echo $birthdays['year'];?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
I think best way making this in SQL to order the data. I mean you can use SQL commands this situation.
You can use ORDER BY;
SELECT * from tableName ORDER BY fieldName ASC/DESC
(in your situation you should use ASC (Ascending))
<?php
$connection = new mysqli('localhost','root','','calendar');
$sql = "SELECT * FROM birthdays ORDER BY birthday_field ASC";
$result = $connection->query($sql);
$birthdayList = $result->fetch_all(MYSQLI_ASSOC);
?>
so you did making them ordered :)
Regards
$sql = "SELECT * FROM birthdays ORDER BY STR_TO_DATE(CONCAT(year, '-', CONCAT(month, '-', date)), '%Y-%m-%d') ASC";
The better approach is to use combined columns for date - YYYY-MM-DD instead of taking 3 columns separately. For this do the following:
In mysql database, create a column of type DATETIME. Store complete date as YYYY-MM-DD
I think you should use one field to keep birthday.
Not day, month, year. You keep all together like 1987-12-17 in table
your field type should be DATE;
birthday => DATE
then you can order them what you want by using SQL
SELECT * from birtdays ORDER BY birthday ASC
well if you get each day, month, year; you have 2 options to do that;
using PHP
using SQL
using PHP;
<?php
// 1987-12-17
$birth = explode("-",$birthdays['birthday']);
$day = $birth[2];
$month = $birth[1];
$year = $birth[0];
?>
<td><?php echo $day;?></td>
<td><?php echo $month ;?></td>
<td><?php echo $year ;?></td>
using SQL;
SELECT DAY(birthday) as day, MONTH(birthday) as month, YEAR(birthday) as birthday from birthdays ORDER BY birthday ASC
again you can get them by using php;
<td><?php echo $birthdays['day'];?></td>
<td><?php echo $birthdays['month'];?></td>
<td><?php echo $birthdays['year'];?></td>
I read your images and mocked up a fiddle demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=369bb7f53c2e5f190d5727c6453c892e
I didn't bother to match your column types, but they are of no real consequence for me to show the resultset from my query. In truth, you really shouldn't be storing the year value as a float -- this just adds more preparation work before displaying to the end user.
The following query will select only the columns that you intend to use and orders the resultset first by month (ascending), and second by day (ascending). The year value is not factored in.
SELECT `person`, `day`, `month`, `year` FROM `birthdays` ORDER BY `month`, `day`
Code:
<!doctype html>
<html>
<head>
<title>Verjaardagskalender</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if (!$conn = new mysqli("localhost", "root","","calendar")) {
echo "Database Connection Error"; // don't show this publicly: $conn->connect_error;
} elseif (!$result = $conn->query("SELECT `person`, `day`, `month`, `year` FROM `birthdays` ORDER BY `month`, `day`")) {
echo "Syntax Error"; // don't show this publicly: $conn->error;
} elseif (!$result->num_rows) {
echo "Possible Logic Error: No Rows";
} else {
echo "<table>";
echo "<tr><th>Persoon</th><th>Dag</th><th>Maand</th><th>Jaar</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>{$row['person']}</td>";
echo "<td>{$row['day']}</td>";
echo "<td>{$row['month']}</td>";
echo "<td>{$row['year']}</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
I am creating a function to show how many users are online now. This is based on who has opened a page within the last 5 min. Each page load is saved to my DB, below:
At the moment I have the following code
$query = mysql_query("SELECT user_id, timestamp FROM user_actions WHERE timestamp > date_sub(now(), interval 5 minute)");
$onlineUsers = mysql_num_rows($query);
This is simply totalling the number of rows, how can I do this so it only counts a user_id once? (so in the above database snippet it should be 2 not 5)
use DISTINCT keyword
$query = mysql_query("SELECT DISTINCT(user_id), timestamp FROM user_actions WHERE timestamp > date_sub(now(), interval 5 minute)");
$onlineUsers = mysql_num_rows($query);
Since mysql_* is deprecated (php 5 onward) and removed in (php 7). So a mysqli_* example is here:-
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('localhost','username','password','db name');//change credentials here
$online_users = array();
if($conn){
$query = mysqli_query($conn,"SELECT DISTINCT(user_id), timestamp,page FROM user_actions WHERE timestamp > date_sub(now(), interval 5 minute)");
if($query){
while($row = mysqli_fetch_assoc($query)){
$online_users[] = $row;
}
}else{
echo "query error:-".mysqli_error($conn);
}
}else{
echo "db connection error:-".mysqli_connect_error();
}
?>
<table>
<tr>
<thead>
<th>User Id</th>
<th>timestamp></th>
<th>Page Visited</th>
</thead>
</tr>
<tbody>
<?php foreach($online_users as $online_user){?<
<tr>
<td><?php echo $online_user['user_id'];?></td>
<td><?php echo $online_user['timestamp'];?></td>
<td><?php echo $online_user['page'];?></td>
</tr>
<?php }?>
</tbody>
</table>
Note:- If you want to show online user name also then you have to do JOIN query.
change table code accordingly.
It's a sample code. modify it accordingly.
You may use group by, e.g
SELECT user_id, timestamp FROM user_actions WHERE timestamp > date_sub(now(), interval 5 minute) group by user_id;
I got the following problem. I got a mysql field which is type "text" and there are many birthday dates in it.
I want to print the dates out and sort it in my way of date format. I can't change the type in the database because it's related to some user profile value fields.
The value's thats in the database are such this:
1978-12-31 23:59:59
This is my query:
$result = mysql_query("SELECT a.value, b.username from yrt6h_community_fields_values a join yrt6h_users b on a.user_id = b.id where a.field_id = 3 order by a.value;");
And this my example php file:
<table>
<tr>
<td>User:</td>
<td>Datum:</td>
</tr>
<?php
while($row = mysql_fetch_array($result)){
echo '<tr>';
echo '<td>';
echo $row['username'] . ' : ';
echo '</td>';
echo '<td>';
echo $row['value'];
echo '</td></tr>';
}
?>
</table>
I tried all of the date format functions of mysql, but then i got nothing.
For example I tried this:
mysql_query("SELECT str_to_date(a.value, '%d.%m.%Y'), ...
or
mysql_query("SELECT str_to_date(date_format(a.value, '%Y.%m.%d') %d.%m.%Y), ...
But I do that, the echo of the date is empty. Theres nothing to print out anymore. But why? Even if I put this query directly in the database I got NULL for the birthday value.
why dont you try to do it with php like -
echo date('d-m-Y', strtotime($yourDate));//the first paramete is the date format you want and the second is the value you are getting from database.
Reference for more understanding, example and formats.
Almost there!
I would suggest that you first get the data from the db with
mysql_query("SELECT str_to_date(a.value, '%d.%m.%Y')
Then
while($row = mysql_fetch_array($result))
{
$date = $row['date'];
$time = strtotime($date);
$formattedDate = date('Y-m-d', $time);
}
echo $formattedDate ;
Does that make senese?
Have you tried the PHP Date() function?
You could do something like so:
If you're looking for all records that match a specific date:
$timestamp = date('Y-m-d H:i:s'); //get current timestamp
mysql_query("SELECT * FROM `table` WHERE `timestamp` = '$timestamp'");
Or -- If you're trying to select/order by Timestamp:
mysql_query("SELECT * FROM `table` ORDER BY `timestamp`"); //select all order by timestamp
you can use mysql "cast" function.
rewrite the query in such a way:
"SELECT cast(a.value as DATETIME) new_dob,str_to_date(a.value, '%d.%m.%Y') dob from table_nm where cond"
I have a bunch of dates in a database that i want the user to be able to select a specific day from a drop down list.
I tried just listing all the dates, but this doesn't work because it shows duplicate dates.
$query1 = "SELECT order_date FROM orders";
$result1 = mysql_query($query1);
while($row1 = mysql_fetch_array($result1))
{
$dateDB = date("Y-m-d", strtotime($row1['order_date']));
$niceDate = date("d/m/Y", strtotime($row1['order_date']));
?>
<option value="<?php echo $dateDB; ?>"><?php echo $niceDate; ?></option>
<?php
}
?>
How can i grab the lowest date, highest date and just populate the select box with everything inbetween ?
Thanks
I tried just listing all the dates, but this doesn't work because it shows duplicate dates.
If that is the core of the problem then the simplest way to fix it would be to alter your query to use the DISTINCT keyword, in combination with the ORDER BY keywords to get them ordered, like so:
SELECT DISTINCT order_date FROM orders ORDER BY order_date
Which means you'll only get unique dates, and the first date will be the lowest and the last will be the higest, which you can easily add into your while loop.
Edit: Although if you want to do with the highest and lowest, you can use the built in MIN and MAX functions to get the highest and lowest, and then generate them as follows:
<?php
$query = 'SELECT MAX(order_date) AS `highest`, ' .
'MIN(order_date) AS `lowest` FROM orders';
$result = mysql_query($query);
$lowest = strtotime(mysql_result($result , 0, 'lowest'));
$highest = strtotime(mysql_result($result , 0, 'highest'));
$dates = array();
$dateTime = $lowest;
do
{
$newDates = array();
$newDates['db'] = date("d/m/Y", $dateTime);
$newDates['nice'] = date("d/m/Y", $dateTime++);
$dates[] = $newDates;
}
while($dateTime <= $highest);
foreach($dates as $date)
{
?>
<option value="<?php echo $date['db']; ?>"><?php echo $date['nice']; ?></option>
<?php
}
?>
I have a PHP page with a MySQL database with 2 fields that use the TIME type in MySQL. I want to convert those two fields from the 24 hour format (00:00:00) into 12 hour AM/PM format (00:00 AM) using MySQL's DATE_FORMAT('','') but it's not working.
So far, what I have done is created a 3rd and 4th field that also uses the TIME type. I send the 1st and 2nd field into the 3rd and 4th and convert the 3rd and 4th to preserve the original.
<?php
//connection statements omitted
$sql="SELECT * FROM $table ORDER BY date LIMIT $start, $amount";
$result = $mysqli->query($sql);
//Sends the original time_in data to the new format_in column.
$sql2="UPDATE $table SET format_in = time_in";
$result2=$mysqli->query($sql2);
$sql3="SELECT DATE_FORMAT(format_in,'%l:%i %p') FROM $table";
$result3=$mysqli->query($sql3);
while($row = $result->fetch_array()){
?>
//other fields omitted
<td><?php echo $row['format_in'];?></td>
//end while loop
<?php } ?>
The only thing this code does is replicates whatever was in the time_in column. Which is the standard 24 hour format 00:00:00. Basically, this code doesn't do anything. What am I doing wrong here?
EDITED to show my $result
while($row = $result3->fetch_array()){
instead of
while($row = $result->fetch_array()){
Based on the conversation we had in comments section:
Change
$sql="SELECT * FROM $table ORDER BY date LIMIT $start, $amount";
$result = $mysqli->query($sql);
to
$sql="SELECT field_1,field_2,field_n,DATE_FORMAT(format_in,'%l:%i %p') format_in FROM $table ORDER BY date LIMIT $start, $amount";
$result = $mysqli->query($sql);