I would like to show the date 2 weeks before the date stored in a DB
The date isnt stored in Timestamp, it is stored like 01/01/2015
I have tried the below but this isnt working, can anyone help?
echo date('$valid_to', strtotime("-2 week"));
I would use DateTime class instead.
// timezone is optional
$date = new DateTime($valid_to, new DateTimeZone('Europe/Vilnius'));
echo $date->modify('-2 weeks');
// there you have your wanted date
$valid_date = $date->format('Y-m-d');
Then would recommend STR_TO_DATE mysql function to convert to correct timestamp.
For example:
$query = "SELECT * FROM table WHERE time_col <= STR_TO_DATE('" . $valid_date . "', '%Y-%m-%d')";
Related
I have the request below in a php file. I'm trying to get a date in this format: MONTHS-currentMonth-currentYear
$sql = "SELECT DAY(ADDDATE(`dateDebutC`, `dureeC`)) AS MONTHS FROM normalW WHERE id = '$id'";
If you want to format the date after returning the values from your sql query, then you can use the PHP date_format() function. Assuming you store the months value in a variable, let's call it $date:
echo date_format($date,'Y-m-d'); // Or whatever format you want to display
To add the current month and current year to that string, you can use the date() function:
$current_month = date(m); // prints current month in numeric format (01-12)
$current_year = date(Y); // prints current year as a 4-digit representation
$new_date = $date . '-' . $current_month . '-' . $current_year;
See https://www.w3schools.com/php/func_date_date.asp for a list of all parameters. And as a friendly reminder, please ensure that you are using mysqli rather than mysql, which is deprecated.
To get current month and year you can use GETDATE function in sql
$sql = "SELECT MONTH(ADDDATE(`dateDebutC`, `dureeC`))AS MONTHS,MONTH(GETDATE()) as curmonth,YEAR(GETDATE()) as curyear FROM normalW where id = '$id'";
Whatever this comes through as in result, for example, however you bind it:
$date = '2014-04-15 10:10:11';
$buildDate = new DateTime(strtotime($date));
$dowMonthDayYear = $buildDate->format('l F j, Y');
echo dowMonthDayYear // turns as April 15th, 2014
You can lookup datetime in php manual for other formats theres tons
Say, I have 4 dates and the date today is 1/7/16:
1/7/16 1:06:02
1/7/16 8:01:24
1/8/16 7:02:23
1/6/16 3:12:34
How can I only pick 1/7/16 1:06:02 and 1/7/16 8:01:24.
What date function from PHP can I use to only get the date of today?
Thanks!
UPDATE:
This is being used as a MYSQL selector.
Example: $db->query("DELETE * FROM entry WHERE date='$today'");
How would this work? How can I get my MYSQL query to select only the dates for today?
UPDATE 2:
I've tried using curdate(), but the code is not working...
This is what I'm doing:
$db->query("DELETE * FROM entry WHERE date=curdate()");
What am I doing wrong?
LAST UPDATE: curdate() worked properly...
UPDATE: As MySQL query, you can do this as follows:
$db->query("DELETE * FROM entry WHERE DATE(date) = CURDATE()");
CURDATE() returns today's date.
If you want to do with php, see below to use the DateTime class.
$now = new DateTime;
$otherDate = new DateTime('2016-01-01'); // or e.g. 2016-01-01 21:00:02
// Setting the time to 0 will ensure the difference is measured only in days
$now->setTime( 0, 0, 0 );
$otherDate->setTime( 0, 0, 0 );
var_dump($now->diff($otherDate)->days === 0); // Today
var_dump($now->diff($otherDate)->days === -1); // Yesterday
var_dump($now->diff($otherDate)->days === 1); // Tomorrow
The answer by schellingerht is totally correct, but
$now = new DateTime;
should be this instead:
$now = new DateTime('Today');
That will ensure that the date from today with 0 hours and 0 minutes is selected. Otherwise the output could be Today even if the date is tomorrow.
Hope that helps!
Convert to timestamp and then format for only the date. Then compare to the same format of todays date:
var_dump(
date('Ymd', strtotime('1/7/16 1:06:02')) === date('Ymd')
);
Try using DATE() (http://www.w3schools.com/sql/func_date.asp). You don't need to convert your dates to a string - this is not efficient.
DELETE FROM yourTable WHERE DATE(yourDateField) = DATE(NOW)
You could compare today's date with Y-m-d format, with your date parsed with the same format.
$today = date('Y-m-d');
$otherdate = date('Y-m-d', strtotime('2022-02-02 16:00:26'));
if($today === $otherdate){
\\ your code for Today
} else {
\\ your code for not Today
}
Been a long while since I posted here, I have done a LOT of coding without having to ask but I am stuck :(
I have a variable which is called "$date", it is always the current date. Now I have logs which have a date set to them, now what I want to do is delete any logs that are older than a week from the current date. Here's how I'm doing it (? means I don't know what to put).
$date = date('Y-m-d H:i:s');
$one_week_old = $date - ?;
$clearlog = mysqli_query($con,"DELETE FROM logs WHERE logs.date < '$one_week_old'");
Instead of using another variable, you can just query it directly:
$clearlog = mysqli_query($con,"DELETE FROM logs WHERE logs.date <
dateadd(week,-1,getdate()));
You can do this:
$one_week_old = date("Y-m-d",strtotime("-1 week"));
$one_week_old = date('d-m-Y', strtotime("-1 week"));
$date = date('Y-m-d');
$newdate = strtotime ( '-1 week' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-d' , $newdate );
$query="DELETE FROM logs WHERE logs.date < '".$newdate." 00:00:00 '";
$clearlog = mysqli_query($con,$query);
If you don't need this date information later in PHP, i suggest you to do that directly in the MySql Query. Please consider, that this example will modify just the datepart, not the timepart of the current timestamp. It was't specified exactly if you like to to have whole days deleted or exactly the those data older then 604800 seconds (7 x 24 x 60 x 60 = 604800).
MySQL 5.5 Reference Manual 12.7 Date and Time Functions
mysqli_query($con, "DELETE FROM logs WHERE logs.date < DATE_SUB(NOW(), INTERVAL 1 WEEK)");
If you prefer Oriented Object paradigm, we have:
$oneWeekAgo = (new DateTime('-1 week'))->format('d/m/Y');
Explanation:
Return a new DateTimeObject
Set the timestamp from -1 week
Format to your need.
I have a table with a date field type date.
What I am trying to do is to do a comparison between the date from inside the table and the today date. If the date from the table is yesterday then insert the today date.
The thing I'm not sure about is how to insert the data in the database so I can make the comparison. here is what im thinking to do"
$d = time();
$x = mysql_querry("SELECT date FROM table where id = $id", $con);
while($y = myqsl_fetch_array($x)){
$oldTime = $y['date'];
}
if ($oldTime < $d){
$i = mysql_querry("INSERT INTO table (date) VALUES (CURDATE()) ", $con);
}
So, I'm not sure if $oldTime and $d can be compared like that, but I hope you guys get my point.
Any ideas?
Thanks
You can't do in that way because the CURDATE() function return a date in a format like 2011-11-11 while time() returns the number of seconds since the January 1 1970 00:00:00 GMT.
Anyway you can change the format of the time() to look like the CURDATE() using the date() function in this way:
date('Y-m-d', time())
or even better, to get the current date, you can use just this line:
date('Y-m-d')
To conclude, you can do the if in this way:
if( strtotime($oldTime) < strtotime(date('Y-m-d')) )
or even better:
if( strtotime($oldTime) < strtotime('now') )
To compare dates you can use strtotime($date); Where date can be a time(), mysql date or date('Y-m-d'); string
I was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));