How can I compute the next Xth day (of the month)? [closed] - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to compute the next Xth day in the calendar.
For example today is 2012-10-08. if X=25 i want it to return 2012-10-25 but if X=06 i want 2012-11-06. If the month doesn't have the required X days, it must return the last day of the month (if i'm looking for the 30th of february for example, it must return 28 or 29 if leap year)
It seems simple enough but I get caught by all the special cases (last month of the year, 28-31 day months, etc).

You can use strtotime() and t:
$x = 5; // given day
if(date('t') < $x){ // check if last day of the month is lower then given day
$x = date('t'); // if yes, modify $x to last day of the month
}
$month = date('m'); // current month
if(date('d') >= $x){ // if $x day is now or has passed
$month = $month+1; // increase month by 1
}
$year = date('Y'); // current year
if($month > 12){ // if $month is greater than 12 as a result from previous if
$year = date('Y')+1; // increase year
$month = 1; // set month to January
}
if(date('t', strtotime($year.'-'.$month.'-01')) < $x){ // check if last day of the new month is lower then given day
$x = date('t', strtotime($year.'-'.$month.'-01')); // if yes, modify $x to last day of the new month
}
$date = date('d F Y', strtotime($year.'-'.$month.'-'.$x));
// 05 November 2012
HERE is a nice tutorial.

Related

Looping through days [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to return all days between a specific range.
My idea was to convert the start and end date to unix timestamp and loop through them adding 86400 (seconds of a day):
<?php
$start = strtotime('2013-01-01');
$end = strtotime('2013-02-01');
for($i=$start; $i<=$end; $i+86400)
{
echo date("l, d.m.y", $i) . "\n";
}
?>
Unfortunately, I only get the same day:
Tuesday, 01.01.13
Tuesday, 01.01.13
Tuesday, 01.01.13
...
The best practise is to use the DatePeriod class.
$start = new DateTime('2013-01-01');
$end = new DateTime('2013-02-01');
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $date) {
echo $date->format("l, d.m.y\n");
}
This is wrong:
for($i=$start; $i<=$end; $i+86400)
should be
for($i=$start; $i<=$end; $i+=86400)
Notice the += insetad of + of your original code. In your code, you didnt assign new value to variable, just perform mathematical formula without result

Get Date after adding month and leave above days [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
$start_date = "2012-01-05";
$end_date = "2012-02-10";
How to get month and after getting month date should like in above example
$end_date should "2012-02-05"; leave above 5 days. don't count a above day.
How can i do this in php?
I wan't to like
$month = 1;
$end_date = "2012-02-05";
Second Example
$start_date = "2012-06-19";
$end_date = "2012-09-25";
then should
$month = 3;
$end_date = "2012-09-19";
Leave days between "2012-09-19" to "2012-09-25".
Thanks
$start = '2012-01-23';
$months = 12;
$date=new DateTime($start);
$date->modify('+'.$months.' month');
echo $date->format('Y-m-d');
You can use strtotime() to just add a number of months to your original date...
$start = '2012-01-23';
$months = 3;
$timeExpr = sprintf('+%d months', $months);
$end = date($start, strtotime($timeExpr));
echo $end; //will output '2012-04-23'

Subtracting Time in PHP to get Difference in Seconds [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to figure out the difference in time by seconds. I keep getting 0 as a results and wants to know if someone could assist me. The times will be pulled from a database.
Here is an example of what I want to be able to do.
$TimeStart = "04:30:45 am";
$TimeEnd = "04:31:46 am";
$Difference = $TimeEnd - $TimeStart . "seconds";
I know this is not a very illustrated example and I know it is probably not that simple.
Here is the code that is currently on my site now:
$TimeStart = strtotime($VisitsRow['TimeStart']);
$TimeEnd = strtotime($VisitsRow['TimeEnd']);
$TimeDifference = ($TimeEnd - $TimeStart);
if ($VisitsRow['TimeEnd'] == "") {
$Timeto = "Incomplete";
} else {
if ($TimeDifference <= 60) {
$Timeto = "< a minute";
} else if ($TimeDifference <= 0) {
$Timeto = "< a second";
} else {
$Timeto = round($TimeDifference, 2)." seconds";
}
if ($TimeDifference >= 60) {
$MinutesDiff = $TimeDifference / 60;
$Timeto = round($MinutesDiff, 2)." minutes";
}
}
I have simply tried the example of what I want above and am getting a negative number. This is why I am needing assistance.
Thank you everyone for helping me solve this. My problem was that it was gathering the times in 24 hr format and not the standard. Once I solved this everything works perfectly.
It's pretty easy with strtotime:
$TimeStart = strtotime("04:30:45");
$TimeEnd = strtotime("04:31:46");
$Difference = ($TimeEnd - $TimeStart) . " seconds";
But, if you need it to be more dynamic, through SQL, you can subtract the two date fields with this:
SELECT TIMESTAMPDIFF(MINUTE,TimeEnd,TimeStart) AS TimeDifference FROM myTable
(Note you can change MINUTE to another time metric if you want)

PHP Create a yearly automated schedule [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to create a yearly automated schedule for a publication. Every issue has 3 dates (Wednesday, Friday & Monday), that occur every two weeks. And in some cases when the day falls on a holiday it should automatically adjust and goto the following day.
I'm trying to do this all right now in PHP, I've got most of it working but after issue 7 the script goes wrong and seems to skip a week. Can anyone help or suggest a way of achieving this?
Here is pseudo-code of my logic;
For 12 months
Get the amount of days in a given month
For days in a month
If Wednesday
assign date to a var
Else if Friday
assign date to a var
print wednesday var
print friday var
print monday var
increment counter in order to skip ahead to the next week
Else if Monday
assign date to a var
Thanks
I think this should point you in the right direction (untested, but should do what you want).
$iYear = date('Y');
for($i = 1; $i <= 12; $i++) {
$iNumDaysInMonth = date('t', mktime(0,0,0,$i,1,$iYear);
for($j = 1; $j <= $iNumDaysInMonth; $j++) {
$iDayNum = date('N', mktime(0,0,0,$i,$j,$iYear));
if($iDayNum == 3) {
// wednesday
} elseif($iDayNum == 5) {
// friday
} elseif($iDayNum == 1) {
// monday
}
}
}

How to display a formatted weekdays from a given Week number [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm getting the weekdays from a given week number and year using the php below:
$week_number = 42;
$year = 2014;
for($day = 1; $day<=7; $day++)
{
echo date('m/d/Y',strtotime($year."W". $week_number.$day));
}
The Output look likes this:
10/13/2014
10/14/2014
10/15/2014
10/16/2014
10/17/2014
10/18/2014
10/19/2014
How can I make it look just like this:
Oct 13 - Oct 19.
Thank you.
<?php
$week_number = 42;
$year = 2014;
echo date('M d',strtotime($year."W".$week_number . 1)) . " - " . date('M d',strtotime($year."W".$week_number . 7)).".";
?>
$week_number = 42;
$year = 2014;
$week_number = ($week_number < 10) ? '0'.$week_number : $week_number;
echo date('M d',strtotime($year.'W'.$week_number.'1')).' - '.date('M d',strtotime($year.'W'.$week_number.'7')).'.';
// remember that $week_number must be prefixed with 0 if week number is lower than 10
you can use strftime
here is the link http://php.net/manual/en/function.strftime.php
Then use
date("F j, D");for your date function

Categories