I need to check date + 1 month from DB with correct date. I try:
$todayDate = date("Y-m-d");
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month");
$result = mysql_query('SELECT UNIX_TIMESTAMP("date") AS date FROM followform WHERE id = "28"') or die(mysql_error());
$date = mysql_fetch_assoc($result);
if ($dateOneMonthAdded == $date['date']) echo 'nice';
But $date['date'] == 0
If I use - mysql_query('SELECT date FROM followform WHERE id = "28"')
$date['date'] == 2012-08-13
Where is my mistake?
UNIX_TIMESTAMP("date") should be UNIX_TIMESTAMP(date). With the quotes in there, it's trying to convert the literal string "date" to a timestamp.
Remove double quotes " from unix_timestamp function in your query. It should be:
SELECT UNIX_TIMESTAMP(date) AS date FROM followform WHERE id = "28"
Related
I have a table (db_dates) with three columns (id, datetime and name_date).
I want to delete one row, when the date is over, such likes this:
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE '".$rowTime[0]."' < '".$today."'");
}
Not working, how do I do that? It is always deleted all data!
I think this will help you.
Because as per your code date will be always less then today's date the way you are getting.
So you should try below code using DATEDIFF().
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE DATEDIFF('".$rowTime[0]."', NOW()) < 0");
}
I tried getdate() function, made a string and converted to date and did an update query but it won't work (I'm new to php)
$date = getdate();
$mydate = $date['mon']."/".$date['mday']."/".$date['year'];
$time = strtotime('$mydate');
$newformat = date('Y-d-m');
$sql = "UPDATE product SET p_date =".$newformat. "WHERE p_id = 2";
It won't update, may be the query is wrong, I just want to update table with the system date.
Like you said in php this is the format
Correct format for a MySQL DATETIME column is
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
Try this
$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO table (datePosted) VALUES ('$date')");
Take a look at the manual.Hope this helps.
date_default_timezone_set('Asia/Kolkata'); // set your timezone
$date = date("Y-m-d H:i:s");
$sql = "UPDATE product SET p_date ='$date' WHERE p_id = 2";
the function should be
date() not getdate()
<?php
$date = date('Y-m-d');
$time = date("H:i:s", time());
echo "$date or $time";
?>
Working Version
<?php
$datetime = date('Y-m-d') . " - " . date(" H:i:s", time());
$sql = "UPDATE product SET p_date = $datetime WHERE p_id = 2"";
?>
I have saved rows in my table with custom timezone.Now I want to retrieve data from those table for just today.
So here is what I tried
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
And my row contains date in timestamp format like 2015-12-07 22:42:02
But I get empty result.
Try this:
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE DATE(date) = CURDATE()" );
to convert time according to timezone: ConvertTimeZone
if $today='2015-12-07 22:42:02'; your query will give the result.
$today='2015-12-07 22:42:02';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
else do pass the today date and next date and retrieve the value as given
$today='2015-12-07';
$next='2015-12-08';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` >= '$today' and `date` <= '$next' " );
for more details refer this Oracle SQL : timestamps in where clause How to compare Timestamp in where clause
You should convert date to timestamp before passing it to mysql:
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = outputs("SELECT * FROM `table` WHERE DATE_FORMAT(date,'%Y-%m-%d')= $today" );
I'm trying to develop a simple php based schedule ...
This is my database table:
Let's say that current time/date is: 2012-03-21 02:00:00
PHP script should echo: Meeting 2
I have a part of the script, but no idea what to do next
<?php
$con = mysql_connect("localhost","root","");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("schedule", $con);
$current_time = date("Y-m-d H:i:s");
$result = mysql_query("SELECT * FROM events");
while($row = mysql_fetch_array($result)){
echo $row['Subject'];
}
mysql_close($con);
?>
How to filter query, to mach current time and scheduled subject? Thank you!
(if current_time is between StartTime and EndTime - echo it's Subject)
Try this query
SELECT * FROM events WHERE NOW() BETWEEN StartTime AND EndTime
Try select * from events where StartTime <= NOW() and EndTime >= NOW()
NOW() will give you current time in mysql.
Please try the following query
select * from events where current_time between startTime and endTime
Your query should like this
$result = mysql_query("SELECT * FROM events WHERE current_time between startTime and endTime);
The mysql BETWEEN operator may be of some use.
$startDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:00:00');
$endDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:23:59');
$sql = 'select subject from events
where startTime between "'.gmdate("Y/m/d H:i:s", $startDate) .
'" and "' . gmdate("Y/m/d H:i:s", $endDate).'") ';
Firstly, you're going to have to convert your StartTime and EndTime into a timestamp using strtotime so you can compare it to the current time (same thing goes for current time):
http://php.net/manual/en/function.strtotime.php
Then just say if current_time > StartTime AND current_time < EndTime echo Subject.
Something like this should work:
while($row = mysql_fetch_array($result)){
$startTime = strtotime($row['StartTime']);
$endTime = strtotime($row['StartTime']);
if (strtotime($current_time) > $startTime && strtotime($current_time) < $endTime
echo $row['Subject'];
}
putenv("TZ=Asia/Calcutta"); // To Set TimeZone
$now = date("Y-m-d H:i:s");
$sql = "select * from events where '$now' between StartTime and EndTime";
I'm trying to select records which have a recdate within the past year
$goodate = date('Y-m-d h:i:s', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
$sqlstmt = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate.'"' ;
but I'm getting no records.
What am I doing wrong?
$sqlstmt = "SELECT * FROM #__mytable
WHERE id=".$uid."
AND recdate > ADDDATE(CURDATE(), INTERVAL -1 YEAR)";
If you want to do this programmatically, the DateTime object is a great tool.
$date = new DateTime();
$gooddate = $date->sub(DateInterval::createFromDateString('1 year'));
$sql = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate->format('Y-m-d').'"';
This is assuming your date field is mysql type DATE. If not, there are similar DateTime methods for outputting unix timestamps, and the format() method accepts any formatting string that date() does.
$sql = "
select *
from #__mytable
where
id = $uid
and
date_format(recdate, '%Y') = date_format(adddate(curdate(), interval -1 year), '%Y')
"