Hi I'm not sure how get this equation correct because I not sure about the dates. I know it returns the same date but I'm trying to learn different formats and how to add dates different days. I know may not be the best way of writing the code I still would like to know how to write this code. The equation is :
$mhYr + ($vSDday -1day)
My code:
$date = "2015-11-02";
$vStartDate = strtotime($date);
$currentdate = $vStartDate;
$vSDday = date('d', $currentdate); //day
$monthY = date('m', $currentdate); //Month
$mhYr = date("01-$monthY"); //first day of any month
But I keep getting errors because of the dates.
An example would be 1stOfMonth + (daysOfStartDate -1 day). So if I started on the 2 November i want to perform : 1st Nov + (2 - 1day). But I don't know how to put all together.
When you do :
$date = "2015-11-02";
$vStartDate = strtotime($date);
and try to print it ( echo $vStartDate), it will print number of seconds difference between 1 january 1970 at midnight and 2015-11-02 , to convert the time to human readable format you need getdate($vStartDate ), it will return collection format.
$date = "2015-11-02";
$vStartDate = strtotime($date);
$timeCollection= getDate($vStartDate);
Then you can start doing :
$day = $timeCollection["mday"]; //day
$month = $timeCollection["mon"]; // month
$year = $timeCollection["year"]; // year
$hour = $timeCollection["hours"]; // hours
$minute = $timeCollection["minutes"]; // minutes
$seconds = $timeCollection["seconds"]; // seconds
Related
I am trying to subtract X days from a date retrieved from MySQL.
X is a variable that can be any integer.
I tried:
$days = trim(explode("days",explode("Order will be late by",$resPO['EXCEPTION_MSG'])[1])[0]); // returning a number 1, 2, 3, etc
$date = $resPO['DUE_DESIRED_RECV_DATE']; //date from database
$reqdate = date('Y-m-d', strtotime("-$days days", strtotime($date)));
Resulting: 04/30/2021
No matter what the date is inserted into the formula. HOWEVER, when I manually change the formula to a constant, it does calculate properly.
Example:
$reqdate = date('Y-m-d', strtotime("-3 days", strtotime($date)));
EDIT:
The $days is working fine when I use a manual date like "2021-04-30". So, looks like the problem is the date I am getting from MySQL.
It is set as DATE and is printing properly.
EDIT 2:
// FORMATTING DATE
$days = $resPO['ORDER_PROJ_LATE_BY'];
//$days = 3;
$date = $resPO['DUE_DESIRED_RECV_DATE']; //date from database
$new_date = explode("-",$date);
$var_new_date = date("Y-m-d", mktime(0,0,0,$new_date[1],$new_date[2] - $days,$new_date[0]));
$newdate = date('Y-m-d', strtotime("-$days days", strtotime($var_new_date)));
EDIT 3:
I found a workaround by multiplying the DAYS (int) from MySQL by 3 and then dividing the results by 3.1 (closer as possible to 3).
// FORMATTING DATE
$days = $resPO['ORDER_PROJ_LATE_BY'];
$days = ($days * 3)/3.1;
Any help?
your code is alright, can it be that the date read from MySQL - in $resPO['DUE_DESIRED_RECV_DATE'] - has an unconventional formatting? strtotime expects them to be formatted in one of the recognized formats.
$days = 3;
$date = '2021-05-01';
$reqdate = date('Y-m-d', strtotime("-$days days", strtotime($date)));
echo "$reqdate\n";
returns
2021-04-28
I have a situation like this. I have a string stored in DB, like this:
$string: 1,1,1,1,1,1,0
It means: Sunday is 0 (a holiday), all the other days (Mon...Sat) are 1.
Then I have two dates: today (ex: 2012-09-07) and some end date (2012-10-04).
I wonder what should I do to get the total number of business days (deducting the holidays) between Today and this End Date (23 days in my example)? We should use the string stored in the DB.
$workdays = explode(",", "1,1,1,1,1,1,0"); // turn string to array (Monday to Sunday)
$days = 0;
$time = strtotime("2012-09-07");
$end_time = strtotime("2012-10-04");
while ($time < $end_time)
{
$day_of_week = date("N", $time) - 1; // 0 = Monday, 6 = Sunday
if ($workdays[$day_of_week] == 1)
{
$days++;
}
$time = strtotime("+1 day", $time); // add one day to time
}
This should work for you. You may need to modify it based on whether it's inclusive, when it should start, etc.
Have you tried date_diff() ?
http://php.net/manual/en/function.date-diff.php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP DateTime::modify adding and subtracting months
I have a starting date (i.e. 2011-01-30) and want to add 1 month.
The problem is in defining what a month is. So if I use the following code:
$d1 = DateTime::createFromFormat('Y-m-d H:i:s', '2011-01-30 15:57:57');
$d1->add(new DateInterval('P1M'));
echo $d1->format('Y-m-d H:i:s');
I get the following result: 2011-03-02 15:57:57
The problem is, that I need it to use the following rules:
If I add 1 month it will just add 1 on the month part and leave the day part
(2011-01-15 will become 2011-02-15)
If the day is not existing in the month we will end, we take the last existing day of it
(2011-01-30 will become 2011-02-28)
Is there a common function in php that can do this or do I have to code it by myself?
Maybe I'm just missing a parameter or something!?
It seems that there is no ready function for it, so I wrote it by myself.
This should solve my problem. Thanks anyway for your answers and comments.
If you will find some errors, please provide in the comments.
This function calculates the month and the year I will end in after adding some month. Then it checks if the date is right. If not, we have the case that the day is not in the target month, so we take the last day in the month instead.
Tested with PHP 5.3.10.
<?php
$monthToAdd = 1;
$d1 = DateTime::createFromFormat('Y-m-d H:i:s', '2011-01-30 15:57:57');
$year = $d1->format('Y');
$month = $d1->format('n');
$day = $d1->format('d');
$year += floor($monthToAdd/12);
$monthToAdd = $monthToAdd%12;
$month += $monthToAdd;
if($month > 12) {
$year ++;
$month = $month % 12;
if($month === 0)
$month = 12;
}
if(!checkdate($month, $day, $year)) {
$d2 = DateTime::createFromFormat('Y-n-j', $year.'-'.$month.'-1');
$d2->modify('last day of');
}else {
$d2 = DateTime::createFromFormat('Y-n-d', $year.'-'.$month.'-'.$day);
}
$d2->setTime($d1->format('H'), $d1->format('i'), $d1->format('s'));
echo $d2->format('Y-m-d H:i:s');
You have several alternatives besides DateInterval.
Here are examples that use strtotime():
http://www.brightcherry.co.uk/scribbles/php-adding-and-subtracting-dates/
// Subtracting days from a date
$date = "1998-08-14";
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
...
// Subtracting months from a date
$date = "1998-08-14";
$newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Here's are some links for DateInterval:
http://php.net/manual/en/dateinterval.construct.php
What can go wrong when adding months with a DateInterval and DateTime::add?
Q: Exactly how do you define a "month"?
Def#1): a "month" is a "month" - regardless of #/days
Def#2): a "month" is 30 days (for example)
Def#3): a "month" is the #/days between the 1st Monday of
subsequent months
etc. etc
Q: What exactly is your "definition"?
OK - as far as I can tell, you still haven't clearly defined what you mean by "month".
But here's one more function that might help:
http://php.net/manual/en/function.getdate.php
/* Function to find the first and last day of the month from the given date.
*
* Author Binu v Pillai binupillai2003#yahoo.com
* #Param String yyyy-mm-dd
*
*/
function findFirstAndLastDay($anyDate)
{
//$anyDate = '2009-08-25'; // date format should be yyyy-mm-dd
list($yr,$mn,$dt) = split('-',$anyDate); // separate year, month and date
$timeStamp = mktime(0,0,0,$mn,1,$yr); //Create time stamp of the first day from the give date.
$firstDay = date('D',$timeStamp); //get first day of the given month
list($y,$m,$t) = split('-',date('Y-m-t',$timeStamp)); //Find the last date of the month and separating it
$lastDayTimeStamp = mktime(0,0,0,$m,$t,$y);//create time stamp of the last date of the give month
$lastDay = date('D',$lastDayTimeStamp);// Find last day of the month
$arrDay = array("$firstDay","$lastDay"); // return the result in an array format.
return $arrDay;
}
I have data in my database that returns the month/day/year data points for events.
What I want to do is check whether today is 20 days after the month/day/year that I get.
So far I have something like:
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
My question is how do I compare it? And is the format going to matter in comparing the two dates?
Thanks!
Parse the month/day/year into a DateTime:
$other = date_create($year.'-'.$month.'-'.$day);
Add 20 days to it:
$twenty_days_after = clone $other;
$twenty_days_after->modify('+20 days');
Compare it with today:
if ($today >= $twenty_days_after) {
// today is 20 days after the month/day/year that you got
}
See date_create.
$database_date = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
$twenty_days_from_now = strtotime('+20 days');
$twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));
Using strttime is the easiest method.
use
strtotime()
function which will convert your dates into timestamp and you can compare them easier
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
//date is 20 days before today
}
strtotime does magical things
I want to get the date for current day in php. what i tried is here...
echo $x."<br>";
echo date("D",$x)."<br>";
But the output was
21-02-10
Thu
It is giving correct date but not the correct day value.Why..?
What I want day is the date for monday for the current week which can be generated on any day of the week. so what I did was, I'm taking the today's day and comparing with (Mon,Tue.... Sun) and respectively creating a timestamp using
case "Mon":
$startdate1=date("d-m-y");
$parts = explode('-',$startdate1);
$startdate2 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+1),$parts[2]));
$startdate3 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+2),$parts[2]));
$startdate4 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+3),$parts[2]));
$startdate5 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+4),$parts[2]));
$startdate6 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+5),$parts[2]));
$startdate7 = date('d-m-Y',mktime(0,0,0,$parts[1],($parts[0]+6),$parts[2]));
$dates=array(1 => $startdate1,$startdate2,$startdate3,$startdate4,$startdate5,$startdate6,$startdate7);
$i=1;
while( $i <= 7 )
{
echo $dates[$i];
$i++;
}
break;
$date is the final array respective to today that has to be returned. Is there any other better method to do this operation.
I tried this to get current day.
echo date('l'); // output: current day.
How about this:
//today is monday
if (1 == date('N')){
$monday = time();
}else{
$monday = strtotime('last Monday');
}
for ($i = 0; $i < 7; $i++){
echo date('d-m-Y', $monday) . '<br>';
$monday = strtotime('tomorrow', $monday);
}
First find Monday, if it is not today, then print 7 dates
What I want day is the date for monday
for the current week which can be
generated on any day of the week.
That's what you want. $mday is the month day of this week's Monday. Nevermind if it's not positive, mktime will handle that right. $monday has the timestamp of the Monday's midnight.
$now = getdate();
$mday = $now['mday'] - ($now['wday'] + 6) % 7;
$monday = mktime(0, 0, 0, $now['mon'], $mday, $now['year']);
echo(date('d-m-y', $monday));
What i did to resolve it is used the date format ('d-m-Y') instead of ('d-m-y') in date function, which was causing the problem. Hence strtotime accepted the format and gave the correct result for
$t=date('d-m-Y');
echo date("D",strtotime($t));
I use the function date and path to it the "D" that refere to the current day , and it works with me
$today = date("D");
and to get the full info about the current date
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
what i tried is here...
echo date("D",$x)."<br>";
date expects a timestamp (int) value as the second parameter. Your $x is a string containing an ambiguous date format. Convert that date into a timestamp first, using strptime or strtotime and use the date function correctly to get the correct day value.
Regarding your second part, you don't need to (and shouldn't) check the day name to calculate the correct Monday, Tuesday etc. A more efficient approach is for example using strtotime to get last Monday etc.
You are likely passing a string as timestamp
echo $x."<br>";
echo date("D",$x)."<br>";
Remove $x and it will output the correct day or change it to
$x = '21-02-2010';
echo date('D', strtotime($x));