How to display table data by today's date? - php

I have a database called gameschedules and a table called dec1212. Inside of this table are the columns date (YYYY-MM-DD), division, field, time, team1, and team2 in that order.
I already have the data displaying on my page but I need to restrict it to only showing rows in my database that have a date that is equal to the current date. So if there are 10 rows and only 5 of them have today's date in the date column, only those should show. Here is my current query code and I know it is wrong but if someone can help me correct it that would be great:
Current code:
//specifies that it is getting data from the table and limited num rows
$result = mysql_query("SELECT * FROM `dec1212` LIMIT 0, 10 ") or die(mysql_error());
This is what I tried to do to restrict data by date:
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = $myDate()") or die(mysql_error());
I know that my code is incorrect so this is why I am asking for help.

Try this just a signal line query
$result = mysql_query('SELECT * FROM dec1212 WHERE DATE(CONVERT_TZ(date,"+00:00","-8.00")) = DATE(CONVERT_TZ(UTC_TIMESTAMP(),"+00:00","-8.00"))') or die(mysql_error());**

$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM `dec1212` WHERE date = '$myDate'") or die(mysql_error());
Try this

$result = mysql_query("SELECT * FROM `dec1212` WHERE `date` = CURDATE()") or die(mysql_error());

Try This
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = $myDate") or die(mysql_error());

You are using variable not the function so just change like this
$myDate = date('Y-m-d');
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = '$myDate'") or die(mysql_error());

Make sure your date field in database containing the only date, not time
$result = mysql_query("SELECT * FROM 'dec1212'
WHERE `date`= '$myDate'") or die(mysql_error());
if your date field containing datetime datatype then use:
$result = mysql_query("SELECT * FROM 'dec1212'
WHERE `date`= date('$myDate')") or die(mysql_error());

Let the database do the work,
SELECT * FROM `dec1212` WHERE DATE(date) = DATE(NOW())

Note that $myDate is a variable, not a function. Thus, your code would be :
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM `dec1212` WHERE date = '$myDate'") or die(mysql_error());
Table name or field name should be wrapped with carat `, not single quote ' or you may omit it.
If you want to use MySQL NOW(), you need to consider time zone conversion as Raj Mohan mentioned.

Related

Convert RSS pubdate to timestamp in mysql database

My table feeds in mysql has a RSS timestring column named pubDate.
I would like to add an additional column pubDate_Date and insert the RSS timestring as a real date.
I have created the additional column formatted as DATE and try to use the following code to update the new column with data, but somehow I cannot get it working. Am a newbie here.
function pubDateToMySql($str){
return date('Y-m-d H:i:s', strtotime($str));
};
$sqlCommand = "SELECT * FROM feeds ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$id = $row["id"];
$pubDate = $row["pubDate"];
$pubDate_Date = pubDateToMySql($pubDate);
$sql = mysqli_query($myConnection, "UPDATE feeds SET pubDate_Date =
$pubDate_Date WHERE ID = $id");
}
mysqli_free_result($query);
To complete this post, this is how I ended up doing it:
UPDATE feeds SET pubDate_Date = STR_TO_DATE(pubDate, '%a, %d %b %Y %T')
In this SQL query
UPDATE feeds
SET pubDate_Date = $pubDate_Date
WHERE ID = $id
you must use date value in ' like this:
UPDATE feeds
SET pubDate_Date = '$pubDate_Date'
WHERE ID = $id
also check type of column pubDate_Date in database

How to generate an employee id with year of registration in PHP?

I need to generate an employee id for eg: E13001, the number should be auto incremented, and when the next year comes, it should be E14001(year is 2014).It should again start from 001 for every year. I have tried with yy format but the sequence number continues from the previous year. How i suppose to my change the sequence number generation ? help me. Thanks in Advance.
Well, the auto-increment on a column in MySQL will not work except for integers. Also it will increment with each field.
To do what you want, you need to separate the id and year in tow (for the mysql table). So you get a table with id,emp_id(should not be managed by the db, no AUTO-INCREMENT) and year. Then select max(emp_id) where year ='2014', so when adding a new employee you will increment that value. To get the employe id the way you need it you should merge the tow columns into one and dump the first tow chars from the year.
You should know that this then becomes a distributed e_id, and every time you need to work with it you will need to do extra work. But you keep the format you need.
Hope this helps you get an idea of how it can be done.
Hi you can try in the following way
<?php
$connection = mysqli_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("test",$connection)
or die("no connection to db");
$query3 = "SELECT * FROM simple";
$result3 = mysqli_query($query3);
$count = mysql_num_rows($result3);
if($count == 0)
{
$year = date('y');
$seq = 1;
$a = sprintf("%04d", $seq);
$emp_id = E.$year.$a;
$query= "INSERT INTO simple (emp_id) VALUES ('$emp_id')";
$result=mysqli_query($query)or die(mysql_error());
}
else
{
$query2 = "SELECT * FROM simple ORDER BY id DESC LIMIT 1";
$result2 = mysqli_query($query2);
$row = mysql_fetch_array($result2);
$last_id = $row['emp_id'];
$rest = substr("$last_id", -4);
$insert_id = "$rest" + 1;
echo $ars = sprintf("%04d", $insert_id);
$year = date('y');
$emp_id = E.$year.$ars;
$query= "INSERT INTO simple (emp_id) VALUES ('$emp_id')";
$result=mysqli_query($query)or die(mysql_error());
}
Hope this code helps you.

Having trouble with MySQL double to date conversion

I have searched and searched for ways to do this but have found very limited information.
I have a MySQL table 'msgdb' that contains a field 'ttime' that is in the format double(25,8) (example row = 1352899856.95249200).
I need to routinely cleanup the table by removing any rows where the field 'ttime' is <= today's date -5 days.
These are the only two lines of code I could find related to double to time conversion but cannot get either to work.
SELECT ADDDATE(ADDDATE(ADDDATE('1899-12-31 00:00:00',FLOOR(ttime)), INTERVAL -1 DAY),INTERVAL(MOD(ttime,1)*86400)SECOND) AS TrueDate FROM msgdb
select date('1899-12-31 00:00:00'+ INTERVAL ttime * 24*3600 SECOND) as date from msgdb
I have tried first to display any rows that match the criteria using the code below, before I started using DELETE FROM to make sure I'm getting the correct results.
$query = "select date('1899-12-31 00:00:00'+ INTERVAL ttime * 24*3600 SECOND) as date from msgdb";
$result = mysql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo $row['date'];
echo '<br>';
}
and also
$query = "SELECT ADDDATE(ADDDATE(ADDDATE('1899-12-31 00:00:00',FLOOR(ttime)), INTERVAL -1 DAY),INTERVAL(MOD(ttime,1)*86400)SECOND) AS TrueDate FROM msgdb";
$result = mysql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo $row['TrueDate'];
echo '<br>';
}
but both are returning nothing.
UPDATE: Ok so by using this code:
$query = "select ttime from msgdb";
$result = mysql_db_query ($dbname, $query, $link);
while($row = mysql_fetch_array($result)) {
echo date('m-j-Y, H:i:s', $row[0]);
echo '<br>';
}
I am able to see it convert 'ttime' field from the stored value of 1352899856.95249200 to 11-14-2012, 07:30:56.
So how would I DELETE from the table all rows where ttime is <=now - 5 days?
Figuring out which records have a date before a point in time should be easy:
DELETE FROM table WHERE ttime <= DATE_SUB(NOW(), INTERVAL 5 DAY);
It might also be better to use UTC_TIMESTAMP() if you store all your times in UTC, which is the only sane way to do it.

MySQL Date_Format isn't working with

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);

Comparing todays date to one listed in the database

I am using mysql and php to pull all the users out of the mysql database that are equal to todays date. The problem I am having is whenever I run the following code I get an error. By the look of the error is seems to be something to do with the year, or maybe it doesn't like the dots in it, but im not sure ?.
<?php
$today = date("d.m.y");
$result = mysql_query("SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe
WHERE date=$today")or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['name'] . " " . $row['email']. " " . $row['date'];
echo "<br />";
}
?>
This is the error it is throwing :
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '.12' at line 2
You have to add quotes around your date string:
$result = mysql_query("SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe
WHERE date='$today'")or die(mysql_error());
Need quotes around your $today variable. Also need to backtick the date column name since it's reserved.
$result = mysql_query("SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe
WHERE `date`='$today'")or die(mysql_error());
First of all, $today should be added as a string into the SQL query:
mysql_query("SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe WHERE `date` = '$today'");
date is a SQL reserved word so it has to be escaped with backticks...
But I recomment using MySQL native functions and constants like NOW(), CURRENT_TIMESTAMP, SYSDATE etc...
SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe WHERE `date` = NOW()
SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe WHERE `date` < NOW() - INTERVAL '2' DAYS
SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe WHERE `date` > NOW() + INTERVAL '5' HOURS
etc...
if you are using timestamp as datatype in mysql then you need to convert the date into unix timestamp first.
$today = strtotime("now");
or if you are using datetime or date data type then correct format is
YY-MM-DD, so you need to create the date accordingly.
$today = ('Y-m-d');
and also in your query you are missing single quote.
"SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe WHERE date = '$today'"
however you can compare the date without using PHP's date function, directly using MYSQL NOW().
SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe WHERE date = NOW()
Try
$today = date("d-m-Y");
OR
$today = date("Y-m-d");
Also, check the format of date column is equal to the format of the return value of date().
If you have used DATETIME data type you may need to use matching format.
Also, wrap date value with single quotes...
$result = mysql_query("SELECT * FROM ymeg_chronoforms_data_NewsletterSubscribe
WHERE date = '" . $today . "'")

Categories