Display a date minus 2 days - advanced-custom-fields

I display a date from a ACF custom field like this:
<?php echo date('j F Y',strtotime(get_sub_field('date') ) ) ; ?>
How would I display the date two days earlier?
Thanks for your help.

Related

MYSQL query select distinct month and year

I have a table with a datetime column with multiple dates in. I want to output the month and year for every row that exists only once. e.g "April 2017, April 2018, May 2018, June 2018"
I've managed to get the below working, this displays "April May June", but it needs to display April twice as it is in different years and I also want to display their respective years.
<?php $results = mysqli_query($link, 'SELECT DISTINCT MONTH(date_added) AS "Month" FROM payments') or die("Query fail: " . mysqli_error($link)); ?>
<?php foreach($results as $result) :?>
<?php $monthText = date("F", strtotime("2001-" . $result['Month'] . "-01")); ?>
<?= $monthText ?>
<?php endforeach; ?>
I've been researching for hours and not manged to find a solution that works.
You can apply the distinct keyword to a combination of fields:
SELECT DISTINCT YEAR(date_added) AS "Year", MONTH(date_added) AS "Month" FROM payments
You have to group:
SELECT MONTH(`date_added`) AS `Month`, YEAR(`date_added`) AS `Year`
FROM `payments`
GROUP BY `Month`, `Year`
With the later versions and strict rules you will not get the right response from either of the above answers as GROUP BY operates before the SELECT part of the query and hence it does not respond to aliases.
You need
SELECT DISTINCT MONTH(`date_added`) AS `Month`, YEAR(`date_added`) AS `Year`
FROM payments GROUP BY MONTH('date_added`), YEAR(`date_added`)

How to get data type date from database?

I have table Birthdays.
Birthdays fields: id | user_id | birthday (type date: y-m-d)
Data from url $_GET['date'] is '1991-09-10'
In BirthdaysController i have a query
$arrayDate = $this->Birthday->find()->where(['birthday' => $_GET['date'] ])->all();
but i want to find by format '1991-09'.
Any help would be greatly appreciated.
Please help me!
I guess what you want to do is to find people with a birthday date for the month of the year given?
First of all, you should use the GET parameters like this $this->request->query('date'); and not like this $_GET['date'].
You'll have to use the between function, so to be sure you're using the first and last day of the month, convert the date like this :
$startDate = $endDate = new \DateTime($this->request->query('date'));
$startDate->modify('first day of this month');
$endDate->modify('last day of this month');
And then you just have to create a query like this
$arrayDate = $this->Birthday
->find()
->where(function($exp) {
return $exp->between('birthday', $startDate->format('Y-m-d'), $endDate->format('Y-m-d'), 'date');
});
They're other ways to use this between find conditions, you should read this.

How get the birthday list of yesterday, today and tomorrow in mysql from the date of birth?

This is the list of date of birth. Now I need to get the ID or each person whose birthday is today, or tomorrow or yesterday.
I have been trying a lot but not getting the right result. Please comment with the proper demo so that it will help me to understand.
My DB structure is:
ID DOB
1 2001-10-15
2 2002-09-13
3 2002-09-11
4 2003-05-09
5 2006-09-12
lets take an ex: Today is 11-09-2015. So I want the result like
ID DOB DAY
5 2002-09-12 Today
3 2002-09-11 Yesterday
2 2006-09-13 Tomorrow
try below query
SELECT DOB,
IF (DAY(DOB)=DAY(NOW())
AND MONTH(DOB)=MONTH(NOW()),
'TODAY',
IF (DAY(DOB) =DAY(ADDDATE(NOW(),-1))
AND MONTH(DOB)=MONTH(ADDDATE(NOW(),-1)),
'Yesterday',
IF (DAY(DOB) =DAY(ADDDATE(NOW(),+1))
AND MONTH(DOB)=MONTH(ADDDATE(NOW(),+1)),
'Tomorrow',
'Future') ) )
FROM tmp;
Output
2015-09-10 Yesterday
2015-09-11 TODAY
2015-09-12 Tomorrow
2015-09-13 Future
Try this code.
<?php
$today=date('Y-m-d');
$yesterday=date('Y-m-d',strtotime("-1 days"));
$tomorrow=date('Y-m-d',strtotime("+1 days"));
$sql=mysql_query("select id from tablename where dob='$today' OR dob='$yesterday' OR dob='$tomorrow'");
while($row=mysql_fetch_array($sql))
{
echo $row;
}
?>

mySQL generating rows based on a recurring date

I have a mySQL table which contains events, details on the event, the event date and a recurring value - which is how often the event recurs in days.
e.g. Date: 2012-12-03, Recurrance: 7 - it will recurr every 7 days.
I am now looking to search on this by date and would like the row to be returned if the search was for 2012-12-03, 2012-12-10, 2012-12-17, 2012-12-24...+7 days.
Currently, the search is for a specific day:
SELECT
id,
title,
venue,
date_format(datetime, '%I:%i%p') AS time
from
bt_db_eventcal
WHERE
DATE(datetime) = '" . $date . "'
Can anyone help expand this to return rows based on the recurrance?
Thanks
its a simple math really. you subtract the saved date from the search date and mod the value by the recurrence. if the value is 0 then it is good.

how to get mysql rows on month basis

I am in a problem and i am confused. I have a table for events and in it there is a column that holds the timestamp of the date time when it was created. I want to get all events(rows) that were created in this month, the last month, and 2 more month before the last month. So, basically 4 months. I want to show them separately so, i can think i can query the datebase for all rows in this month in one query and another query for previous month and so on for 4 months. The problem is i don't know how can i do that. There is a month() function in mysql but i am lost, no idea how to use it.
Any help would be very much appreciated. Thanks
Edit: table structure is like this.
Title | Uri | created
Event | Event| 1337782223
name | uri |
.......
EDIT 2
Thanks all for your help..
I tried it myself and with my friend google...:)
I used this
SELECT * FROM `events` WHERE MONTH(FROM_UNIXTIME(created)) = $month;
and this seems to work for me, i will just pass the month whos rows i want from php thats easier for me...lol
Thanks again to all for your answers its appreciated. This place is awesome
Select * From your_table Group By Month(column_with_date);
Of couse you have to have a timestamp column in your table.
If not then it's impossible to restore the informations when the entry was inserted.
$month = '5,4,3,2';
select colum_name from table_name where month(table_field_name) IN ($thismonth) Group By month(table_field_name)
try this one if you are using separate query for each month
$todayDate = date("Y-m-d");
$thismonth = strtotime($todayDate);
$previous_month = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
and you can get past months just do minus NUMBER ex -2,-3 .....for current month
select colum_name from table_name where month(table_field_name) = month ($thismonth) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name);
for previous month
select colum_name from table_name where month(table_field_name) = month ($previous_month ) AND year(table_field_name) = year ($thismonth) Group By month(table_field_name)