Convert RSS pubdate to timestamp in mysql database - php

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

Related

Get Data From a Query With Several Cases

I have a search field with 3 fields (2 field for date range and 1 field for person). I would like if I select the person, then the data based on this person to appear on the screen. Same as I am using the while loop below.
When I select the date range, only the data for this date range to appear on the screen. I am able to do that using the following query.
$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];
$employer = $_POST['employer'];
$sql = "
SELECT *
FROM jobs
WHERE employer_id = '$employer'
OR (endDate BETWEEN '$startDate' AND '$endDate')
";
$res = mysqli_query($db, $sql) or die(mysqli_error($db));
I would also want if I select both the date range and the person field, then I would like the data for both of them to appear on the screen with the while loop shown below.
I want this to be achieved using the query above, but I do not have a solution.
Then, I extract all data in a while cycle:
while($row = mysqli_fetch_assoc($res))
{
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$title = $row['job_title'];
$date = $row['endDate'];
}

Get date and time of max value

I have a weather-reading database where I store all data for consumption by a webpage that shows the MAX and MIN pressure.
The problem is that I like to show the time and date in which these values where recorded.
Code I use to read MAX pressure:
$max_out_pressure = mysql_query("SELECT MAX(out_pressure) AS out_pressure FROM $table");
$max_out_pressure = mysql_result($max_out_pressure,0,"out_pressure");
$max_out_pressure = substr($max_out_pressure, 0, 6);
echo "$max_out_pressure";
echo "mbar";
I have the columns ID, Datetime, Pressure, Temperature
Table is named readings
Webpage: http://temperatur.co.nf/readings.php
Never made an database or webpage before, so I`m struggling with this.
This will get the maximum pressure and the date it was recorded.
$sql = "SELECT out_pressure, Datetime
FROM $table
ORDER BY out_pressure DESC
LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$max_out_pressure = $row['out_pressure'];
$max_date = $row['Datetime'];

Select a string from mysql table in php doesn't show output

I'm trying to extract a string field, under a column called "whenadded", of a mysql table (called "values") with a php script. I'm using this code:
<?php
include("common.php");
$link=dbConnect();
$name = safe($_POST['name']);
$mysqldate = mysql_query("SELECT `whenadded` FROM `values` WHERE `name`= `$name`");
?>
In this table I have only two columns: name and whenadded. Both are strings.
If I try to display the result of $mysqldate, I don't see anything (white field).
[SOLVED] At the end I solved using this:
include("common.php");
$link=dbConnect();
$name = safe($_POST['name']);
$query = mysql_query("SELECT * FROM $dbName . `valorigps` WHERE name = '$name'");
while($row = mysql_fetch_array($query))
{
echo $row['whenadded'];
}
your query string shoud be this:
$mysqldate = mysql_query("SELECT whenadded FROM values WHERE name= '$name'");
or
$mysqldate = mysql_query("SELECT whenadded FROM values WHERE name= '{$name}'");
no need of quote for table columns!

How to display table data by today's date?

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.

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

Categories