PHP/SQL Converting timestamp - php

So I am working on a simple website and I ran into a problem. I have a subscription based website and I have a date expired for when their subscription ends. This all works well, but when I tried to display the expiration date I ran into problems. The first 3 lines are what i have been trying. It seems as if the timestamp isnt correctly being transferred from the database because when I did my test at the button, this displayed the correct date. The top 3 lines always give me this: 1970/01/01
// Get Expiration Date
// Always gives me 1970/01/01
$datexpire = "SELECT date-expire FROM users WHERE username='{$_SESSION['username']}'";
$timestamp = mysqli_query($link, $datexpire);
$date = date("Y/m/d",$timestamp);
//This works
$timestamp2 = 1537847863;
$date2 = date("Y/m/d",$timestamp2);
If anyone could help that would be much appreciated

i think your code should be something like
$datexpire = "SELECT date-expire FROM users WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($link, $datexpire);
$row=mysqli_fetch_assoc($result));
$timestamp = $row['date_expire'];
$date = date("Y/m/d", $timestamp);
echo $date;
please check for column name.. if it is date-expire or date_expire ??? (dash or underscore ??)

mysqli_query returns a mysqli_result object or a boolean value.
You want to fetch a row from your given object, like so:
$datexpire = "SELECT `date-expire` FROM users WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($link, $datexpire);
$row = mysqli_fetch_assoc($result);
$date = date("Y/m/d", $row["date-expire"]);

Related

How to pass a date variable into a php query

I've been trying many different ways to pass the dates into the query as a variable, and nothing I've tried has worked. When I put the exact dates in there like below, the query works fine.
$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "2017-01-01" AND "2017-12-31"');
while($row1 = mysqli_fetch_array($ly))
{
$ly_cash = $row1['SUM(rtype_price)'];
}
For example, I've been trying something like this:
$ly_start = date('Y-m-d', strtotime('first day of previous year'));
$ly_end = date('Y-m-d', strtotime('last day of previous year'));
then passing $ly_start and $ly_end into the query like this:
$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "$ly_start" AND "$ly_end"');
Inverting the quotes did the trick. This works:
$ly = mysqli_query($link,"SELECT SUM(rtype_price) FROM client WHERE paid = 1 AND date BETWEEN '$ly_start' AND '$ly_end'");
while($row1 = mysqli_fetch_array($ly))
{
$ly_cash = $row1['SUM(rtype_price)'];
}
Thank you ild_flue

mysqli prepared statements with between clause

My question is similar to [this][1] but the solution provided there doesn't work for me.
I have a DB and I have one column called birth that contains a date in the format year-day-month 00:00:00. Now I want to extract from the DB all the records which birth is between two dates.
The birth value has been inserted in the DB with this code
$date = $day . "-" . $month. "-" . $year;
$a = strptime($date, '%d-%m-%Y');
$DB_date = date('Y-m-d H:i:s',mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900));
To extract the dates I am using this code:
$stmt = $conn->prepare('SELECT * FROM database WHERE `birth` BETWEEN ? AND ?');
$start_date = '1990-01-01';
$stop_date = '1995-01-01';
$stmt->bind_param('ss',$start_date,$stop_date);
$stmt->execute();
$result = $stmt->get_result();
but it doesn't find anything so $result->num_rows is equal to 0 and it doesn't give any errors so the problem must be in the comparison of the dates.
Birth column format is y-d-m hour min sec. But in your where clause you are ignoring hour minute second factor.
Replace:
$start_date = '1990-01-01';
$stop_date = '1995-01-01';
With this:
$start_date = '1990-01-01 00:00:00';
$stop_date = '1995-01-01 00:00:00';
Kindly try it and let me know if it works
I found the solution
The code in my question works if the data type of the column in the DB is datetime and you also need to make sure that $start_date is older than $stop_date. I still wonder though why it didn't work with the date data type.
I had the same issue. I found another thread with the same problem (not sure how to duplicate this thread) here
However just to sum up,
They managed to get the desired results by bracketing the question marks for the params.
$stmt = $conn->prepare('SELECT * FROM database WHERE `birth` BETWEEN (?) AND (?)');
$start_date = '1990-01-01';
$stop_date = '1995-01-01';
$stmt->bind_param('ss',$start_date,$stop_date);
$stmt->execute();
$result = $stmt->get_result();
Hope this helps.

mysqli query does not update in while loop

I am building a script to display some 24 hour charts. I am attempting to make 12 charts. One 24 hour (last hour) chart for each five minute data set i have collected. I am running accross an issue where my mysql command will not update the $hour_position. I've read many while loop inside while loop questions and have tried to construct a foreach alternative, and also tried to reset the query array, but none of this has worked. Any comments or links to another thread that can solve this problem would be appreciated.
Thanks in advance.
$hour_position = 00;
$htime = -1;
$date = date('Y-m-d H:i:s');
$date = strtotime($date);
$date = strtotime($htime." day", $date);
$date = date('Y-m-d H:i:s',$date);
while($hour_position < 60){
$price_history_qry = mysqli_query($con,"SELECT * FROM `dbTable` WHERE `server_time` > '$date' AND EXTRACT(MINUTE FROM `server_time`) like $hour_position ORDER BY `server_time` ASC");
while($result = mysqli_fetch_array($price_history_qry)){
//Create a couple of arrays to build a chart.
}
//Build chart here
echo $chart;
$hour_position = $hour_position +05;
}
Try putting $hour_position in quote like
' $hour_position '
or you can use
". $hour_position ."
Your query has a small mistake. The variable inside single quotes will not be evaluated as variable. Check this. you need to change the select query like below.
$price_history_qry = mysqli_query($con,"SELECT * FROM `dbTable` WHERE `server_time` > '".$date."' AND EXTRACT(MINUTE FROM `server_time`) like ".$hour_position." ORDER BY `server_time` ASC");
And i haven't find any update query in your code. As you said you are using the update query inside while loop. So first check whether the variable has the value and then check whether it entering to while loop like below.
$result = mysqli_fetch_array($price_history_qry)
var_dump($result);
while($result){
//Create a couple of arrays to build a chart.
var_dump("entered");
}
Try to use var_dump() function to debug.
Hope it helps you.

PHP displaying date is one day forward after format

When I retrieve a date from a my_sqli query and format it it becomes one day forward. The date is saving correctly to the server, and echoing it before the new format is correct.
$date = "SELECT date FROM blogtable WHERE id = $artID";
$dateEx = mysqli_query($con, $date);
while($dateGet = mysqli_fetch_array($dateEx))
{
//This is in YYYY-mm-dd
$dateGet['date'];//If I echo this, it is correct
}
$source = $dateGet;
$newDate = new DateTime($source);
echo $newDate->format('M-d-Y');
So for example if I tried to use it today(the 24th), it would save correctly, but after the format, display as the 25th.
Wrikken's suggestion worked, changing the statement in the while to $source=$dateGet['date']; and deleting the $source = $dateGet;.

Failing to compare php date with mysql date

Got stuck in a complex or maybe stupid problem. I am getting a query from mysql, and then trying to compare a date column with a PHP data which i formatted to the same format i.e "Y-m-d" it always results in no match, although i see there is a match.. and it gets the right result set too.
<?php
date_default_timezone_set('America/Los_Angeles'); // set timezone to our timezone
$constantTime = time(); // get value of time in constant
$appDate = date("Y-m-d", $constantTime); //that defines php time variable -
$queryDate = "SELECT * FROM date WHERE date='$appDate'";
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resulDate);
if ($appDate == date("Y-m-d", strtotime($recordDate['date']))) {
echo "MATCH ";
$dateID = $recordDate['dateID'];
} else {
mysql_query("insert into date(date) values('$appDate')")or die("Database write error1");
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resultDate);
echo "NO MATCH ";
$dateID = $recordDate['dateID'];
}
This is always triggering the else, i tried === instead of ==, i tried strcmp
As i assume you're comparing datetime field, you have two possibilities:
Cast field to date:
$queryDate = "SELECT * FROM your_table WHERE date(your_date_field) = date('$appDate')";
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
or
Modify your date format to be ISO compatible:
$appDate = date("Y-m-d H:i:s", $constantTime); //it defines date in format 2015-03-14 15:00:00
$queryDate = "SELECT * FROM your_table WHERE your_date_field='$appDate'";
See also this question

Categories