dd/mm/YYYY format date - while loop problems adding 1 day - php

I am creating a while loop in PHP to iterate through all the days between two dates.
$startdate = "17/12/2022"; // my dd/mm/YYYY format start date
$startdate = str_replace('/', '-',$startdate); // convert the slashes to dashes to signify european date format
$startdate = strtotime($startdate); // convert the date into a date object
$enddate = "16/12/2023"; // my dd/mm/YYYY format start date
$enddate = str_replace('/', '-',$enddate); // convert the slashes to dashes to signify european date format
$enddate = strtotime($enddate); // convert the date into a date object
echo(date('d/m/Y',$startdate)."<br/>");
echo(date('d/m/Y',$enddate)."<br/>");
while($startdate <= $enddate) {
$dayofweek = date('w',$startdate); // a task
echo($dayofweek)."<br/>");
//I need to increment the $startdate by one day here?
}
?>
I believe that changing the slashes to dashes effectively instructs PHP to recognise the date as dd/mm/YYYY rather than mm/dd/YYYY. There are references (in many examples) to the PHP documentation outlining this functionality but the PHP documentation no longer shows this; nor does it show that it is depleted or has been changed. It is simply omitted. My tests show that it works but I cannot find reference to an alternative.
I then convert both dates to a date object using strtotime so that they can be compared.
I then need to add 1 day to the $startdate and loop, but it is the adding one day that is foxing me.
All the examples I can find are using todays date or a string not formatted dd/mm/YYYY.
I freely admit that I may have got the whole premise wrong but any pointers for good practise would be appreciated.

Using the DateTime class and the DateInterval Class and the DatePeriod class you get quite a bit of the heavy lifting done for you
$startdate = "17/12/2022"; // my dd/mm/YYYY format start date
$enddate = "31/12/2022"; // my dd/mm/YYYY format start date
$sDate = ( new Datetime() )->createFromFormat('d/m/Y', $startdate);
$eDate = ( new Datetime() )->createFromFormat('d/m/Y', $enddate);
$oneDay = new DateInterval('P1D');
$daterange = new DatePeriod($sDate, $oneDay ,$eDate);
foreach ( $daterange as $dat) {
echo $dat->format('d/m/y - w D') . PHP_EOL;
}
RESULTS using a smaller range of dates
17/12/22 - 6 Sat
18/12/22 - 0 Sun
19/12/22 - 1 Mon
20/12/22 - 2 Tue
21/12/22 - 3 Wed
22/12/22 - 4 Thu
23/12/22 - 5 Fri
24/12/22 - 6 Sat
25/12/22 - 0 Sun
26/12/22 - 1 Mon
27/12/22 - 2 Tue
28/12/22 - 3 Wed
29/12/22 - 4 Thu
30/12/22 - 5 Fri

Related

Adding days to date, outputting incorrect dates? [duplicate]

I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01
When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

Date in a URL dd/mm/yyyy

I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01
When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

PHP: get next date based on fixed base date

is there a way in PHP to get the next date(s) using a 4-week interval from a given date ?
Example:
My start date is Friday, Jan 03, 2014 and my interval is every 4 weeks from that date.
What I am looking for is the next date (or dates, if possible) from the current date that matches this 4-week interval.
In the above example this would be Friday, May 23, 2014 (then June 20, 2014, July 18, 2014 etc.).
I know I can get the current date as follows: $today = date('Y-m-d');
and I could probably set the start date like this: $start = date('2014-01-03');
but I don't know how to calculate the interval and how to find out the next matching date(s).
You should read up on the DateTime classes, specifically DatePeriod and DateInterval:
$start = new DateTime('2014-01-03');
$interval = DateInterval::createFromDateString('4 weeks');
$end = new DateTime('2015-12-31');
$occurrences = new DatePeriod($start, $interval, $end);
foreach ($occurrences as $occurrence) {
echo $occurrence->format('Y-m-d') . PHP_EOL;
}
DatePeriod takes a start date and a DateInterval and allows you traverse over the object to get all dates within the boundaries using the given interval. The cut off can be either a set number of cycles (so the next 10 dates) or an end date (like above), even if the end date is not one of the dates the interval falls on (it will stop below it). Or you can use an 8601 interval notation string (which sounds so much fun, huh?), but I'm pretty shaky on that.
If 4-week interval means 7 x 4 = 28 days, you can obtain the "next date" by:
$today = new DateTime();
$next_date = $today->add(new DateInterval('P28D'));
$next_next_date = $next_date->add(new DateInterval('P28D'));
$next_next_next_date = $next_next_date->add(new DateInterval('P28D'));
And if you want to calculate more "next dates", you can repeat the add() to repetitively add 28 days to your date.
Note: Beside using P28D, you can use P4W, which means 4 weeks.
While some answers may suggest using strtotime(), I find the object-oriented approach more structured. However, DateInterval is only available after PHP >= 5.3.0 (while DateTime is available after PHP >= 5.2.0)
You could use strtotime()
echo date('Y-m-d', strtotime('now +4 weeks'));
UPDATED:
$start = date('Y-m-d', strtotime('2014-01-03 +4 weeks'));
echo $start;
You could also run this in a for loop to get the next 6 or more dates. For example:
$Date = "2014-01-03";
$Int = 6;
for($i=0; $i<$Int; $i++){
$Date = date('Y-m-d', strtotime('{$Date} +4 weeks'));
echo $Date;
}

formatting mysql timestamp in php, with several conditions

I'm trying to format a SQL timestamp in PHP based on the following conditions, but can't figure out how. Can anyone point me in the right direction?
If the timestamp was TODAY, display as 4:15PM or 12:30AM
If the timestamp was before TODAY but in the past 7 DAYS, list as 'Sunday' or 'Monday'
If the timestamp was before 7 DAYS ago, list as 'mm/dd/yy'
How would I go about that?
First you need to convert the MySQL time to a unix timestamp which is what most of php date functions use. If you are using MySQLs DateTime type, you can perform the conversion in SQL with the MySQL function unix_timestamp() mysql date functions. Or you can convert the mysql date to a unix timestamp in PHP with the strtotime($mysqlDateTime) function php strtotime function
once you have the unix timestamp of the time you would like to format, the conversion would look something like this (86400 is number of seconds in 24 hours):
function displayDate($timestamp)
{
$secAgo = time() - $timestamp;
// 1 day
if ($secAgo < 86400)
return date('h:i:A', $timestamp);
// 1 week
if ($secAgo < (86400 * 7))
return date('l', $timestamp);
// older than 1 week
return date('m/t/y', $timestamp);
}
This method has the benefit of not requiring extra object creation in PHP (a tad slow) or performing unnecessary calculations on the SQL server. It might also help to know that MySQL's timestamp type stores data as a unix timestamp (number of seconds since Jan 1 1970) value requiring only 32bits for storage compared to datetime which uses 64bits of storage. 32 bits should be enough for everyone, until 2038 or something....
you can check date difference by by diff() of PHP or by msql datediff()
http://www.php.net/manual/en/datetime.diff.php
Then check difference is zero or equal to 1 or greater than 7
h 12-hour format of an hour with leading zeros 01 through 12 date('H:i:s')
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
G 24-hour format of an hour without leading zeros 0 through 23
USE DATE(G) to find AM or PM
if($TODAY)
date('h:i:s')PM
ELSE IF ($THISWEEK)
l (lowercase 'L') A
full textual representation of the day of the week Sunday through Saturday
ELSE IF($BEFOREONEWEEK)
date('d-m-y')
http://php.net/manual/en/function.date.php
This should work. Hope so :-)
You just have to use a conditional:
$now = new DateTime("now");
$ystrday = new DateTime("yesterday");
$weekAgo = new DateTime("now")->sub(new DateInterval('P7D'));
$inputDate = new DateTime(whenever);
if($yesterday < $inputDate and $inputDate < $now){
$outDate = date('g:ia', $inputDate->getTimestamp() );
}else if($weekAgo < $inputDate and $inputDate < $now){
$outDate = date('l', $inputDate->getTimestamp() );
}else if($inputDate < $weekAgo){
$outDate = date('d/m/y', $inputDate->getTimestamp() );
}
This hasn't been tested and you'll need to get your mySql date into a php DateTime object but it should get you pretty close.
I assume you're talking about the MySQL TIMESTAMP datatype, since I don't think MySQL actually has a datatype like a Unix timestamp (i.e. seconds since epoch), so you'll have to first convert the date you get using the strtotime function:
$timestamp = strtotime($dbTimestamp);
This will return a Unix timestamp you can play with.
Next we'll define a couple more timestamps to compare this value against:
First, we want to know the timestamp for midnight this morning. For that, you'll pass the string "today" to strtotime:
$today = strtotime("today");
Next, we need to know the timestamp for seven days ago. You'll have to choose between "1 week ago" and "1 week ago midnight". The difference between these two is that midnight will return the timestamp for 12am on that day, while the version without it will return the current time, seven days ago (e.g. today, the difference would be that midnight will return 12 AM on April 7 and the non-midnight version would, right now, return 3:45PM on April 7):
$weekAgo = strtotime("1 week ago midnight");
(Note, there are many formats that strtotime understands, including many relative formats like the "today" and "1 week ago" examples used above.)
Next, we need to define the date formats to use in each case:
$timeOnly = "g:i A"; // This gives an "hour:minute AM/PM" format, e.g. "6:42 PM"
$dayOfWeek = "l" // Gives a full-word day of the week, e.g. "Sunday"
$mdy = "m/d/Y" // gives two-digit month and day, and 4-digit year,
// separated by slashes, e.g. "04/14/2011"
Finally, we just do our comparisons, and format our timestamp using the date function:
if ($timestamp >= $today) {
$date = date($timeOnly, $timestamp);
} elseif ($timestamp >= $weekAgo) {
$date = date($dayOfWeek, $timestamp);
} else {
$date = date($mdy, $timestamp);
}
This will leave you with a string variable called $date which contains your database-provided timestamp in the appropriate format, which you can display on your page as needed.

Set start and end date for last 30 days ending today

I need to generate two dates in the format YYYY-MM-DD example: 2010-06-09
The end date should be today and start date should be today - 30 days.
How can I generate these 2 dates in the above format?
for the last 30 days so end date is
today and start date is today - 30
days
The strtotime is real friend:
echo date('Y-m-d', strtotime('today - 30 days'));
It's very simple with DateTime class. You can simply pass the string with the relative expression to the constuctor. When you need to get the date as a string in a specific format use format() method.
$endDate = new \DateTime();
$startDate = new \DateTime('-30 days');
// when you need to use them simple format as a string:
echo $endDate->format('Y-m-d');
echo $startDate->format('Y-m-d');

Categories