{"id":412,"date":"2011-06-26T11:33:26","date_gmt":"2011-06-26T10:33:26","guid":{"rendered":"http:\/\/blog.stratus.org.uk\/?page_id=412"},"modified":"2011-06-26T11:47:16","modified_gmt":"2011-06-26T10:47:16","slug":"php-list-urls-by-day","status":"publish","type":"page","link":"https:\/\/blog.stratus.org.uk\/?page_id=412","title":{"rendered":"PHP: List URLs by Day"},"content":{"rendered":"<p>A recent problem arose when trying to provide a dynamic list of linked URLs on a page based on the textual name rather than the the numeric date. A number of sub-pages on a website has unique but not ordered URLs, suffixed with a page number (numeric).<\/p>\n<p>The problem was compounded by the fact that the target links, while part of the same site, where not in ascending order. And obviously it would be nicer if we start with today and work forwards.<\/p>\n<p>This is what we started with:<\/p>\n<ul>\n<li>Monday (which points to 39)<\/li>\n<li>Tuesday (which points to 40)<\/li>\n<li>Wednesday (which points to 38)<\/li>\n<li>Thursday (which points to 41)<\/li>\n<li>Friday (which points to 42)<\/li>\n<li>Saturday (which points to 36)<\/li>\n<li>Sunday (which points to 37)<\/li>\n<li>Today (which points to 35)<\/li>\n<\/ul>\n<p>We actually want:<\/p>\n<ul>\n<li>Today<\/li>\n<li>Tomorrow<\/li>\n<li>The day after that<\/li>\n<li>The day after that<\/li>\n<li>\u2026<\/li>\n<li>The day after that and<\/li>\n<li>The last day<\/li>\n<\/ul>\n<p>And we also want to include the date for the day listed as well so it\u2019s clear what day it refers to.<\/p>\n<p>The trick used was to create an array of days, indexed by the day name (e.g. Monday), and then work forward in a fixed loop using the PHP <em>date <\/em>function, to determine the real day name and date. We use the PHP\u00a0<em>strtotime<\/em> function with a string that says it is one or more days ahead, which returns the date which in turn allows the <em>date <\/em>function to correctly return the textual day name. So we get the actual date (e.g. Jun 26), we do that as well using the same method. Once we have both results, glue them together as the visual representation of the HTML link (the <a> mark-up).<\/p>\n<p>Note that \u201ctoday\u201d and \u201ctomorrow\u201d are separate from the remainder of the loop, because the suffix is slightly different. But you could lump them all together.<\/p>\n<p>The end result looks like this:<\/p>\n<pre>\/\/ create an array for the days of the week, with the\r\n\/\/  pointer a textual day name\r\n$week = array( \"Monday\"=>39, \"Tuesday\"=>40, \"Wednesday\"=>38,\r\n    \"Thursday\"=>41, \"Friday\"=>42, \"Saturday\"=>36, \"Sunday\"=>37,\r\n    \"Today\"=>35 ); \r\n\r\n$dayname = date('l', strtotime('today'));\r\necho '<a href=\"page.php?'.$week[$dayname].'\">'.$dayname.\r\n    ' Today - 4Km<\/a>';\r\n$dayname = date('l', strtotime('+1 day'));\r\n$daydate = date('d M', strtotime('+1 day'));\r\necho '<br><a href=\"page.php?'.$week[$dayname].'\">'.$dayname.\r\n    ' '.$daydate.' - 5Km<\/a>';\r\n\r\nfor ($day = 2; $day < 7; $day++ )\r\n{\r\n   $dayname = date('l', strtotime('+'.$day.' day'));\r\n   echo '<br><a href=\"page.php?'.$week[$dayname].'\">';\r\n   echo $dayname;\r\n   $daydate = date('d M', strtotime('+'.$day.' day'));\r\n   echo ' '.$daydate;\r\n   echo ' - 12Km<\/a>';\r\n}<\/pre>\n<p>And the actual output looks like this when today is \u201cSunday\u201d and the 26th of June:<\/p>\n<ul>\n<li>Sunday Today \u2013 4Km<\/li>\n<li>Monday 27 Jun \u2013 5Km<\/li>\n<li>Tuesday 28 Jun \u2013 12Km<\/li>\n<li>Wednesday 29 Jun \u2013 12Km<\/li>\n<li>Thursday 30 Jun \u2013 12Km<\/li>\n<li>Friday 01 Jul \u2013 12Km<\/li>\n<li>Saturday 02 Jul \u2013 12Km<\/li>\n<\/ul>\n<p>Note that this isn\u2019t showing the embedded HTML links, but you should get the idea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recent problem arose when trying to provide a dynamic list of linked URLs on a page based on the textual name rather than the the numeric date. A number of sub-pages on a website has unique but not ordered &hellip; <a href=\"https:\/\/blog.stratus.org.uk\/?page_id=412\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":84,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-412","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":8,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/412\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/412\/revisions\/428"}],"up":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/84"}],"wp:attachment":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}