Mysql query on date function - php

I have table "movie" which consists of a field "date of release"
how do I query that, which movie hit's the theatre every friday?

$r = false;
$movie = false;
$r = query("SELECT id FROM movie WHERE dateOfRelease='friday'");
$movieInfo = query("SELECT * FROM movie WHERE id='{$r[\'id\']}'");
Made some assumptions because your question has very little information, hopefully this will help

You can find out this week's friday like this:
$datefriday=date('Y-m-d', strtotime('next friday'));
Assuming you have a date() type field in your database, you can use a query like:
SELECT column1,column2,column3,column4 from movie where date_of_release = $datefriday
I have no idea if you use mysqli, pdo or even mysql. So I cant let the query run in php for you. The query has to look like this though. If you want the friday of the week after that, you can use:
$date_next_friday=date('Y-m-d', strtotime($datefriday.'+ 7 days'));
This should help you.

Related

Check if the time is more than 24h and show it

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

PHP : comparing fetched date with today

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.

What's wrong with my Group By clause?

Sorry for the stupid question, but I can't figure out why my data is displaying twice.
It's a very simple query. The DB table calendar_px currently has just three rows and four fields, including one for the date, another for the year and another (Brief) that includes some brief content.
So, if you visit the page MySite/Calendar/January_1, you should see all the years and briefs from the DB table that have a matching date (Date2) field (January_1). My practice table has three such rows:
1969 | So and so was born.
1969 | A volcano erupted.
1970 | They released a new song.
My code displays the information perfectly - except that it displays twice. I've grouped it by every field name, with and without the Cal2. extension, and nothing works. It looks like I must have made a really simple mistake, but I don't get it.
Here's my query...
$stm = $pdo->prepare("SELECT Cal2.N, Cal2.Date2, Cal2.Year, Cal2.Brief
FROM calendar_px Cal2
WHERE Cal2.Date2 = :MyURL
GROUP BY Cal2.Brief");
$stm->execute(array(
'MyURL'=>$MyURL
));
break;
}
while ($row = $stm->fetch())
{
$Year = $row['Year'];
$Brief[] = ''.$Year.' – '.$row['Brief'].'';
}
And this is what I have on the display page...
echo join( $Brief, '<br>' );
Try this:
$rows= $stm->fetchAll();
while ($rows as $row)
{
$Year = $row['Year'];
$Brief[] = ''.$Year.' – '.$row['Brief'].'';
}
Yikes, I knew I did something stupid, and I finally figured it out. I somehow included the file with the query twice. I have to remember to use require_once rather than a simple require.

How could I echo a random database field once a week using php and mySQL?

Google hasn't been much help sadly. I have some pseudo code below to give you an idea of what I'd like to achieve:
if ($time == one week) {
$result = mysql_query("SELECT * FROM table RANDOMLY,$connection");
echo $result[0];
}
I know I should be using mysqli, but I'm augumenting an existing (ageing) system. I'll be utilising mysqli in future, so if you could give me the solution using mysql that would be great!
I don't think it can be done with a single statement.
My best guess would be to use mysql_list_tables, select a random entry from that list and continue from there on.
Perhaps generating a random number between 1 and the value of SELECT Count(ID) from table. Then you have the index of the value you wish to output, you can simply run a SELECT statement for it; and output! :)
if ($time == one week) {
$result = mysql_query("SELECT * FROM table RANDOMLY,$connection");
echo $result[0];
}
Try this:
$weekNumber = date("W");
$result = mysqli_query("SELECT * FROM table RANDOMLY WHERE weeknumber = '\"$weekNumber\"', $connection");
echo $result[0];
}
Of course you’ll need a column in your table called weeknumber with 1 through 52 setup ahead of time.
As others pointed out you shouldn’t use mysql_* anything. I changed it to a mysqli_query.

Extract a month from a date

I'm trying to build a query with propel Criteria to get all Foo's in a given month.
For example I want all Foo's in March. In regular SQL I would build a query like this:
SELECT * FROM FooPeer WHERE MONTH(startDate) = 3
Any Idea how I can implement the "MySQL Month-function within a Criteria Object" ?
$c = new Criteria();
$c -> add(FooEvent::START_DATE, 3, Criteria::EQUAL); //where do I have to put the Month function ?
return self::doSelect($c);
Alright, a Custom Criteria did the job!
$month = 3; //march
$criteria->add(FooPeer::START_DATE, 'MONTH('.FooPeer::START_DATE.')='. $month, Criteria::CUSTOM);
This question was partly answered there: How to use MySQL functions in Propel
Using Criteria::CUSTOM or writing custom SQL seem to be the only solutions.

Categories