I want to change date var when x days have passed
For instance:
Today is 21.12.16 - $date = '23.12.16'
Tomorrow is 22.12.16 - $date = '23.12.16'
When it's 23.12.16 - $date = '25.12.16'
Her's the code I got so far. Hope this will make some sense
$date = "2016-12-21"; //** will describe this lower
$days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
$new_date = date('d.m.y', strtotime("+2 days"));
} else{
$new_date = $date;
}
This works ok if I just want to do it once
**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php
P.S. sorry for my bad English.
Here's what I came up with:
$date = '2016-12-01'; //Your script start date, you wont need to change this anymore
$everyxdate = 10; // once x days to add x to $date
$days_passed = date_create()->diff(date_create($date))->days; // passed days from start of script $date
$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate; // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want
Hope this will help some one who has the same task I had.
Related
I want to compare current date's day and month with subscription date's day and month only.
For example:
current date(d-m) = 3-6
And I want compare it with any other d-m
How should I do it in PHP
In my project condition is like birth date in which we don't compare year.
The trick in this is to let the month come first. This way PHP can compare the numbers by highest value. Take a look at the following example:
$aDate = DateTime::createFromFormat('m-d', '05-20');
$bDate = DateTime::createFromFormat('m-d', '06-29');
if ($aDate->format('md') > $bDate->format('md')) {
echo "'aDate' is bigger than 'bDate'";
}
use like
$current_date = date("d-m");
$subscription = "03-06-2016";
$subscription_date = date("d-m", strtotime($subscription));
if($current_date ==$subscription_date)
{
echo "date is equal";
}else
{
echo "date is not equal";
}
If you only need to check if the j-n date is the same as the current date, then you don't need to make more than one function call. Because you are not comparing greater than or less than, the format of your input is unimportant.
Code: (Demo)
$subscription = '29-11';
var_export(date("j-n") === $subscription);
// at the moment, the result is true
j is today's day of the month without any leading zeros and
n is today's month without any leading zeros.
Use DateTime() PHP objects.
Considering you have an array with user info from mysql query result: ($userData['suscriptionDate'])
$today = new DateTime();
$userSuscription = new DateTime($userData['suscriptionDate']);
if ( $today->format('d') == $userSuscription->format('d') && $today->format('m') == $userSuscription->format('m')) {
echo 'Congratulations!!';
}
Use DATE_FORMAT() function to extract part of date:
Ref: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
SELECT * from table_name WHERE DATE_FORMAT(subscription_date, '%d-%m') = "05-05";
I think, more elegant way to compare, especially when you have a full date with time is diff function of Datetime class:
$d1 = new Datetime();
$d2 = new Datetime('+3 months +2 days +3 hours');
$diff = $d1->diff($d2);
var_dump($diff->d); // 2
var_dump($diff->m); // 2
// or have a comparison as a string
var_dump($diff->format('Difference is in %R%a days'));
// output: Difference is in 63 days
Enjoy! Link to doc
This may help you
$sdate = $row['subscription_date'];
$date1 = date("m-d");
$date2 = date("m-d",strtotime($sdate)) ;
if ($date1 == $date2) {
}
How do I add a variable number of days to a date in PHP? All I've found is examples calculating with a specific number of days, not a variable.
$date = $row["date"]; // i.e 2015-12-13
$days = $row["days"]; // i.e 10
I want the output (in this example 2015-12-23) to be put in variable $futuredate.
Thanks!
You can try to do this with strtotime() and date() functions
$date = $row['date'];
$days = $row['days'];
$futuredate = date('Y-m-d',strtotime($date) + (24*60*60*$days));
I have on function which passing some parameter the like
everyWeekOn("Mon",11,19,00)
I want to compute the difference between the current day (e.g. 'Fri')
and passed parameter day i.e. Mon.
The output should be:
The difference between Mon and Fri is 3
I tried it like this
$_dt = new DateTime();
error_log('$_dt date'. $_dt->format('d'));
error_log('$_dt year'. $_dt->format('Y'));
error_log('$_dt month'. $_dt->format('m'));
But know I don't know what to do next to get the difference between the two days.
Note that this question is different from How to calculate the difference between two dates using PHP? because I only have a day and not a complete date.
Just implement DateTime class in conjunction with ->diff method:
function everyWeekOn($day) {
$today = new DateTime;
$next = DateTime::createFromFormat('D', $day);
$diff = $next->diff($today);
return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}
echo everyWeekOn('Mon');
$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';
You can find no. of days in two days by using this code
<?php
$today = time();
$chkdate = strtotime("16-04-2015");
$date = $today - $chkdate;
echo floor($date/(60*60*24));
?>
Please use this may this help you
I want to create a function that can find the closest time, based in a string of second.
The system will receive an int number that equivalent of second of that time.
PHP must find the closest (in past) date.
Example:
//supose that an anterior script created it at "14-08-25 10:32:30"
//and now it's "14-08-25 10:33:12"
$seconds = 30; // the variable passed from an anterior script
$time_received= date('Y-m-d H:i:s'); // this is the time that I'll receive this
//so, from these 2 variables above, i must find "14-08-25 10:32:30"
Anyone have an idea how to do this?
I have just these variables:
The time right now, that is "14-08-25 10:33:12"
and the $seconds variable.
With these 2, I want to get "14-08-25 10:32:30"
It isn't completely clear to me what you are trying to accomplish, but I'm making a guess by using strtotime():
<?php
$seconds = 30;
$time = strtotime("-" . $seconds . " seconds");
echo date( "Y-m-d H:i:s", $time );
?>
Found.
I'm using this script:
$seconds = 30;
$new_date = new DateTime(date()); //the only two variables that i have
if($new_date->format('s')<$seconds){
$new_date->setTime($new_date->format('H'),$new_date->format('i')-1,$seconds);
$old_date = $new_date->format('Y/m/d H:i:s');
}else{
$new_date->setTime($new_date->format('H'),$new_date->format('i'),$seconds);
$old_date = $new_date->format('Y/m/d H:i:s');
}
I want to get the $registratiedag and count a couple of days extra, but I always get stuck on the fact that it needs to be a UNIX timestamp? I did some google-ing, but I really don't get it.
I hope someone can help me figure this out. This is what I got so far.
$registratiedag = $oUser['UserRegisterDate'];
$today = strtotime('$registratiedag + 6 days');
echo $today;
echo $registratiedag;
echo date('Y-m-d', $today);
There's obviously something wrong with the strtotime('$registratiedag + 6 days'); part, because I always get 1970-01-01
You probably want this:
// Store as a timestamp
$registratiedag = strtotime($oUser['UserRegisterDate']);
$new_date = strtotime('+6 days', $registratiedag);
// You'll need to format for printing $new_date
echo date('Y-m-d', $new_date);
// I think you want to compare $new_date against
// today's date. I'd recommend a string comparison here,
// As time() includes the time as well
// time() is implied as the second argument to date,
// But we'll put it anyways just to be clearer
if( date('Y-m-d', $new_date) == date('Y-m-d', time()) ) {
// The dates are equal, do something here
}
else if($new_date < time()) {
// if the new date is earlier than today
}
// etc.
First it converts $registratiedag to a timestamp, then it adds 6 days
EDIT: You probably should change $today to something less misleading like $modified_date or something
try:
$today = strtorime($registratiedag);
$today += 86400 * 6; // seconds in 1 day * 6 days
at least one of your problems is that PHP does not expand variables in single quotes.
$today = strtotime("$registratiedag + 6 days");
//use double quotes and not single quotes when embedding a php variable in a string
If you want to include the value of variable $registratiedag right into the text passed as parameter of strtotime, you have to enclose that parameter with ", not with '.