i have a mysql table with date of birth, so an example field would be:
dob
----
1989-01-01
I have pulled todays date in zend framework with the following code:
public function todaysDate()
{
$date = new Zend_Date();
$date = $date->get(Zend_Date::MONTH_SHORT . '-' . Zend_Date::DAY_SHORT);
return $date;
}
This shows current date in format:
9-24
Now i want to search the dob field in my mysql table for anyway with the month 9 and day 24 in their dob disregarding year. how do i do this?
please note the dob is not a date field, its a text field
SELECT ...
FROM ...
WHERE (month(dob) = 9) AND (day(dob) = 24)
Related
I have this php code:
(from my database class)
$q = "SELECT * FROM" . CON_TBL. " WHERE a = $b ORDER BY thedate DESC";
$result = mysqli_query($this->connection, $q);
The dates (unix timestamp) on the DB are:
1138322340
1617584160
1617673680
759952800
I got this result:
30-Jan-1994 12:00
05-Apr-2021 20:48
04-Apr-2021 19:56
26-Jan-2006 18:39
The first date should be the last:
05-Apr-2021 20:48
04-Apr-2021 19:56
26-Jan-2006 18:39
30-Jan-1994 12:00
Any ideas what's going on?
The thedate column was a VARCHAR instead of an INT, once I changed the column to INT the ORDER was corrected.
I am using PHP with MySQL and would like to select rows that have a booking time within 2 hours from now. How do I compare what is in my database with the NOW() MySQL function?
I have columns pickupDate in the format yyyy-mm-dd and pickupTime in the format HH:mm (24-hour). I have tried creating a query with NOW() which returns the a 12-hour time as HH:mm:ss e.g. 2019-05-24 07:54:06 . I can't figure out how to format this to 19:54, or if I should use a different function instead.
For example, if the current date and time is 24/05/19 19:54:06, I would like to select rows between 19:54 and 21:54 on this date.
My table structure is:
referenceNo VARCHAR(100)
pickupDate DATE
pickupTime VARCHAR(100)
You need to create a DATETIME compatible value out of your pickupDate and pickupTime (which you can do by CONCATing them together), then you can compare that with a time range from NOW() to 2 hours later:
SELECT *
FROM yourtable
WHERE CONCAT(pickupDate, ' ', pickupTime) BETWEEN NOW() AND NOW() + INTERVAL 2 HOUR
Demo on dbfiddle
To add two hours in php
$hoursnow = date('H:i');
$timestamp = strtotime(date('H:i')) + 60*60*2;
$plusTwohours = date('H:i', $timestamp);
And $PlusTwohours using this variable frame the query like below
Sql Query:
$sqlQuery = 'select * from foodorder where pickupDate=DATE(NOW()) AND pickupTime>='.$hoursnow.' and pickupTime<='.$plusTwohours;
$result = mysql_query($sqlQuery);
variable $result will have the values of query
For Second Scenario: Adding hours to end of the day May 24 23:30:00
This should be handle by two different date for same column pickupDate
$d = new DateTime('2011-01-01 23:30:30');
$startDate = $d->format('Y-m-d H:i:s'); // For testing purpose assigned manually
$starttime = date('H:i');
// Here Process start, storing end date by adding two hours
$enddate1 = strtotime($startDate) + 60*60*2;
$enddate = date('Y-m-d', $enddate1); // Extracting date alone
$endtime = date('H:i', $enddate1); // Extracting time alone
Have to compare start and end date for column pickupDate, here is the query
$sqlQuery = "select * from foodorder where pickupDate>=DATE(".$startDate.") AND pickupDate<=DATE(".$enddate.") AND pickupTime>='".$starttime."' AND pickupTime<='".$endtime."'";
$result = mysql_query($sqlQuery);
I want to put this format data on my table: 2018-12-04T13:05:00-00:00
This should be done with a query:
$uPqr = $conn->query("UPDATE table SET dateModified = "the date goes here" WHERE id = 25");
I don't want to write the date on the query, i want to know if there's a function like NOW() or time() that do it automatically.
If you want the current time -- on the server -- then just use now():
$uPqr = $conn->query("UPDATE table SET dateModified = now() WHERE id = 25");
I've a builder for selecting people with a phone number not null, a permission flag true (in the first builder call), and with a birthday template not null in this function. There is also a birthday field:
public function getAllForBirthdaySmsSend()
{
$qb = $this->getAllSuitableForSmsQueryBuilder();
$alias = $qb->getRootAlias();
$today = new \DateTime();
return $qb->andWhere(
$qb->expr()->andX(
$qb->expr()->isNotNull("$alias.sms_birthday_template"),
/* Filter if today is his birthday */
))
;
}
Now i should filter people by birthday, that is if birthday column formatted as 'm-d-' . date('Y') equals $today.
Anyone knows how to to this with query builder? I don't want to write pure SQL query but i prefer reusing the other query builder to be DRY.
You can use native sql and then map it in order to deal with real entities makeing use og ResultSetMapping. Of curse, the YEAR is ignored:
Mapping the query fields:
$rsm = new Doctrine\ORM\Query\ResultSetMapping;
$rsm->addEntityResult('models\User', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'username', 'username');
$rsm->addFieldResult('u', 'birthday', 'birthday');
Create a query using native date functions (each selected column now is mapped ):
$query = $this->doctrine->em->createNativeQuery('
SELECT id, username, birthday
FROM user
WHERE TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE)
> TIMESTAMPDIFF(YEAR, birthday, CURRENT_DATE - INTERVAL 1 DAY)', $rsm);
Get the users:
$users = $query->getResult();
Tip: Try to avoid casting column values when you want to do a query. It forces MySQL(if it is your case) to convert the column into a CHAR before performing the comparison and alter the performance.
Edit birthday column is datetime type. And the user model has the birtdate attibute mapped as \DateTime object
PreparedStatement pStmt = con.prepareStatement("insert into mydate values(?,?)");
java.util.Date now = new java.util.Date();
pStmt.setDate( 1, new java.sql.Date( now.getTime()) );
pStmt.setTimestamp( 2, new java.sql.Timestamp( now.getTime() ) );
pStmt.executeUpdate();
QUERY1:
I am creating post a comment module .I want to display the time when user posted their comments.also i want to show the time passed after they have posted a comment.
like 2 days ago ,5 min before etc.
QUERY2:
i also want to change the format of the date to dd/mm/yyyy.
For displaying the date formatted you can use java.text.SimpleDateFormat or
org.apache.commons.lang.time.FastDateFormat
For the time intervals you can use org.apache.commons.lang.time.DurationFormatUtils
Here is a sample:
//this will get the date in format dd/mm/yyyy
FastDateFormat fmt = FastDateFormat.getInstance("dd/MM/yyyy");
String txt = fmt.format(date);
//this will get the interval
long ival = System.currentTimeMilis()-date.getTime();
String interval = DurationFormatUtils.formatDurationWords(ival, true, true);