I like to compare a date (extract from database with a null value (0000-00-00).
I try to do like that:
$dateValue = $row_recordset3['ttDateEnvoiDEP'];
if ($dateValue === '0000-00-00') {
$dateEnvoiDEP = "CURRENT_DATE()";
}
else {
$returnOK .= $dateValue;
$dateEnvoiDEP = $row_recordset3['ttDateEnvoiDEP'];
}
But it doesn't work, do you know why?
why don't you do it directly in your SQL query, something like this,
SELECT IF(ttDateEnvoiDEP = '0000-00-00', CURDATE(), ttDateEnvoiDEP)
as `ttDateEnvoiDEP`,
FROM ...
WHERE ...
CURRENT_DATE() is a MySQL function, not PHP.
Give this a read: http://php.net/date
Also, if you want to use the current date in that format you can do date('Y-m-d') which will return 2012-09-03 or whatever the date is.
If you want to deal with date with PHP you can use unix time stamp for that.
Read php time and date for more information
The problem is your code is you are compareiring 0000-00-00 to $dateValue.0000-00-00 is not null. Not clear what you what here. But I think you want to know if the value is set. so you can try
isset($dateValue)
Hope this could help you!
EDIT:
if(!isset($dateValue)){
$dateEnvoiDEP = time();
}else{
//do other stuff
}
Related
I have in my MSSQL database a column with datatype of datetime which contains some dates in this format 2021-01-11 19:58:04.277.
This is a voting system, the idea is that the users can only vote once every 24 hours.
Every time they vote this table is updated with a new record and a new date is added with the corresponding user.
I want to display a message that says how many hours left to place the next vote.
This is the code I am trying to use:
/**
* Get Votes Time
*
*/
public function getVoteRemainingTime($account) {
date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');
$sql = "SELECT VoteDate FROM dbo.vote WHERE Account = :account ORDER BY logid DESC";
$query = $this->db->prepare($sql);
$query->execute(array(':account' => $account));
$voteDate = $query->fetch(PDO::FETCH_OBJ);
$timeLeftVote = strtotime($currentTime) - strtotime($voteDate->VoteDate);
if($timeLeftVote > 86400) {
return '<strong>Vote Available!</strong>';
} else {
return $timeLeftVote;
}
}
But it is displaying the wrong information. What I am doing wrong? I would appreciate your help.
Thanks!
you need declare format parameter of the date() like date('Y-m-d H:i:s')
date_default_timezone_get();
$currentTime = date('Y-m-d H:i:s');
$timeLeftVote = strtotime($currentTime) - strtotime('2021-01-11 19:58:04.277');
if($timeLeftVote > 86400){
echo 'Vote available';
}else{
echo $timeLeftVote;
}
Instead of SELECT VoteDate FROM dbo.vote
Can you do the calculation on the time difference at source in the database using
SELECT VoteDate, DATEDIFF(HOUR, VoteDate, GETDATE()) as HourDifference from dbo.vote
As I cannot check your database query, I only checked the rest of the code and it seems to work (as Fikri F mentioned in the comments of this post) if I replace $voteDate->VoteDate by a static date.
So please provide more information. You could output the current time and the previous vote time from the database as strings, and for both dates as well the result of strtotime, and in the end the result of the method. Then please explain, what the wrong behaviour is. By this, we can narrow down the problem either to the DB query or to the PHP code.
(I would write this as a comment, but I have not enough reputation.)
i'm saving time for first login ,now when user logs in i enter time using NOW() function, that saves time in this format (data type is DATETIME.
2015-12-24 15:47:30
Now logic is like every login is first login so i've to check if there already exists an entry for today to check that i fetch time explode it and get time like this
$logintime= mysqli_query($connection,"SELECT loggedin from employees");
$loggedin_time= mysqli_fetch_assoc($logintime);
$Date = $loggedin_time['loggedin'];
$loggedin_time_converted= explode(" ",$yourDate) ;
$ConvertedDate = $loggedin_time_converted[0];
last line returns 2015-12-24 now i've date
$today= time();
$DateToday= date("Y-m-d",$today);
$DateToday also returns me same format and same date now i need your help me to compare these dates , if they are equel i dont uopdate database if they are not i will , Pleas help me how do i compare these values
You can do the test in MySQL
$result = mysqli_query($connection, "SELECT DATE(loggedin) = CURDATE() AS logged_in_today FROM employees");
$row = mysqli_fetch_assoc($result);
if (!$row['logged_in_today']) {
// code to update database
}
Wow, you've done all the hard stuff to get the problem to the point of being a simple comparison of 2 strings. This is all you need to do to get over the finish line ...
if ($ConvertedDate !== $DateToday) {
// update the database
}
You can use Php Built In function "Date Difference."
Code Seems Like As Follow:-
$today= time();
$DateToday= date("Y-m-d",$today);
$diff = date_diff($today,$DateToday);
echo "$diff days";
This will return values something like +12 days or something else.
I have the following statement in the where statement of a mysql query:
WHERE scrap_date LIKE '%-01-%'
I want to grab all the data with the scrap_date being in January. The error I recieve is:
"Incorrect datetime value: '%-01-%' for column 'scrap_date' at row 1"
The datatype for scrap_date is DATETIME.
Not sure what syntax to use to get data with the a date in January, any suggestions?
You are assuming the date is represented internally as a string. It is not.
Since its a DateTime, use the MONTH function to extract the month and compare it to the desired value
WHERE MONTH(scrap_date) = 1
You may try this:
select * from your table WHERE MONTH(scrap_date) = 1
You can use the DATEPART() function
SELECT * FROM table
WHERE (DATEPART(mm, scrap_date) = 01)
use month function
WHERE MONTH(scrap_date) = 1
I have a field created_at which is in the format yy-mm-dd hr-min-sec .. now how to select only date part of this field?
function customers(){
$this->datatables
->select('created_at')
->group_by('email')
->from('customers')
echo $this->datatables->generate();
}
How to reg this stuff?
Set the value from the created_at field to $result then
$date = new DateTime($result);
echo $date->format('Y-m-d');
You can do in the query with the SUBSTRING command:
->select('SUBSTRING(created_at,1,8)')
(note the first character starts at index 1)
However it would be better performance-wise to do it in the resulting PHP code.
echo substr($data->created_at,0,8);
Expecting you are using MYSQL
http://dev.mysql.com/doc/refman/5.0/fr/date-and-time-functions.html
function customers(){
$this->datatables
->select('DATE_FORMAT(created_at, '%Y/%m/%d') as view_created_at')
->group_by('email')
->from('customers')
echo $this->datatables->generate();
}
thend you just need to do view_created_at;
You need to use DATE() function and your query should look like
->select("DATE(`created_at`) AS created_at",FALSE) // to avoid additional bacticks
Trying to figure out why my code isn't working. Basically I have an elseif statment like so:
mysql_connect("localhost","xxxx","xxxxx");
mysql_select_db("xxxxxx");
$sql = "SELECT COUNT(DATE) FROM calendar";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$checkdate = $row['DATE'];
$DATEFROM = $_POST['DATEFROM'];
$DAYCOUNT = $_POST['DAYCOUNT'];
$DAYS = $_POST['DAYS'];
if ( $DAYCOUNT < $DAYS ) {
header( 'Location: request_go_fail.php' );
}
else if ( $checkdate == $DATEFROM ) {
echo "FAIL!";
}
else {
It doesn't work, the first check (to see if the DAYCOUNT is less than DAYS works fine, but when comparing to entries in the DB it doesn't seem to do it. Seems to be some issue with finding the already existing data, as when I change $checkdate to an entry that's already in the database it works great.
Any help is most appreciated :)
SELECT COUNT(DATE) FROM calendar doesn't return a field called date, print_r the $row variable to confirm that. Best solution is to change the statement to something like SELECT COUNT(DATE) AS datecount FROM calendar and then do $checkdate = $row['datecount'];
But while rereading your code fragment, I'm not sure that you really want the count of DATE's in the calendar table, and what exactly the intention is, is hard to determine from the code fragment.
Also, DATE is a reserved word in SQL, not the optimal choice for a column name!
Did you try printing $checkdate? I suspect it's null if that is indeed the SQL you're using.
Should be $row['COUNT(DATE)'] I believe, or you can use mysql_fetch_array and $row[0] instead, or use an AS in your SQL or
$checkdate = mysql_result($result, 0);
And skip the fetch call all together.
COUNT(DATE) will return the number of non-null DATE fields in your DB btw, is that really what you want?
You don't have a DATE key in the $row variable because of the sql command. Use this instead, it's called Alias:
SELECT COUNT(DATE) AS DATE_COUNT FROM calendar
Now you have a key DATE_COUNT which will contains value.
$checkdate = $row['DATE_COUNT'];