I am trying to change a date displayed in this format "d/m/Y" into this format "d-m-Y", and I wrote the code below:
//Add $license years to today
$today = date("j-m-Y");
$license = $row_customize['license'];
$startdate = $row_customize['startdate'];
list($d,$m,$Y) = explode('/',$startdate);
$newstartDate = $d."-".$m."-".$Y;
$enddate= date("j-m-Y", strtotime("$newstartDate +$license years"));
echo $today;
echo $enddate;
$today displayed correctly, but $enddate is showing me a date in 1970 e.g 1-01-1970.
Please what have I done wrong?
I would put
$newstartDate = $d."-".$m."-".(intval($Y)+intval($license));
$enddate= date("j-m-Y", strtotime($newstartDate));
I think the format "<date> + n years" is not allowed for strtotime()
The default date is 0 which equates to 01-01-1970. So both $newstartDate and $license years are 0 so when added together the result is still 0. So the questions are why is $newstartDate and $license set to 0.
I think like p91paul said you may not be able to use n Years. I think also $newstartDate may be in an unsupported format. Could you use $startDate in the function and then perform the format change after?
Related
I have the following dates that I get from database as well as from php date. I did some formatting to the date from database to suit accordingly. Those are as follows
From database
$startDate = $tagQueryRows['startDate'];
$startDate = strtotime($startDate);
$startDate = date( 'd-m-Y', $startDate );
Take note that the startDate in database is in this format 2019-09-02 10:26:44
From php date
$todayDate = date('d-m-Y');
Then I did a subtraction as follows
$totalDaysCompleted = ($todayDate- $startDate);
When I did this, it shows the correct number of days but with warning
Notice: A non well formed numeric value encountered
Thus I edited my code as below
$totalDaysCompleted = (strtotime(str_replace('-','/',$todayDate)) - strtotime(str_replace('-','/',$startDate)));
This time the warning gone, but it is not showing the $totalDaysCompleted correctly. The $totalDaysCompleted should be a number like 1, 2, 3 etc. But now it is showing weird number such as -150000 etc.
Can someone help me?
After some trial and errors, I got it right by doing the following way
$startDate = $tagQueryRows['startDate'];
$startDate = strtotime($startDate);
$todayDate = date('d-m-Y');
$totalDaysCompleted = (strtotime($todayDate)) - $startDate;
$totalDaysCompleted = round($totalDaysCompleted/(60*60*24),0);
I need to create a date object that will be changed by parameters that i get.
If I'll get -7 for days it will take me a week ago.
Here is my code. How do I format the date correctly?
public function get_time_get($myear=0,$mmonth=0,$mday=0,$mhour=0,$mminute=0,$msecond=0){
$year=date('y') +$myear;
$month=date('m')+$mmonth;
$day = date('d')+$mday;
$hour= date('H'+$mhour); // there is a bug
$minute = date('i')+$mminute;
$seconds= date('s')+$msecond;
$date=mktime($year,$month,$day,$hour,$minute,$seconds);
$t =date("Y-m-d H:i:s", $date);
debug($date);
}
You can see that I try to get the time , but I get this:2021-11-30 17:08:29
That is not correct
You wrote:
$hour= date('H'+$mhour);
But it should be:
$hour = date('H') + $mhour;
The $mhour should be outside of the date function.
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.
i am struggling for a long time to set a specific date but i am not getting correct out put.
i want to get date from user and compare that date with the date 15 days older then today. if it is older than 15 days then convert to today else print what it is.
$todaydate= $_GET['date'];// getting date as 201013 ddmmyy submitted by user
$todaydate=preg_replace("/[^0-9,.]/", "", $todaydate);
$today =date("dmy"); //today ddmmyy
$older= date("dmy",strtotime("-15 day")); // before 15 days 051013
if ($todaydate <= $older){
$todaydate= $today;}
problem is, it is taking date as number and giving wrong result.
Comparing date strings is a bit hacky and prone to failure. Try comparing actual date objects
$userDate = DateTime::createFromFormat('dmy', $_GET['date']);
if ($userDate === false) {
throw new InvalidArgumentException('Invalid date string');
}
$cmp = new DateTime('15 days ago');
if ($userDate <= $cmp) {
$userDate = new DateTime();
}
Also, strtotime has some severe limitations (see http://php.net/manual/function.strtotime.php#refsect1-function.strtotime-notesand) and is not useful in non-US locales. The DateTime class is much more flexible and up-to-date.
try this one:
<?php
$todaydate = date(d-m-Y,strtotime($_GET['date']));
$today = date("d-m-Y");
$older= date("d-m-Y",strtotime("-15 day"));
if (strtotime($todaydate) <= strtotime($older))
{
$todaydate= $today;
}
?>
$previousDate = "2012-09-30";
if (strtotime($previousDate) <= strtotime("-15 days")) {
//the date in $previousDate is earlier or is equal to the date 15 days before from today
}
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 '.