function getNumberOfPostsThisMonth() { $today = new Timestamp(); $monthTimestamp = $today->getYear() . $today->getMonth(); $query = "SELECT COUNT(*) AS total FROM " . $this->getPrefix() . "articles" . " WHERE date LIKE '{$monthTimestamp}%' AND status = 1"; return $this->getTotalFromQuery($query); }
/** * generates an archive link given a date. * * @param date A Timestamp object * @return A valid archive link */ function getArchiveLink($date) { $archiveLinkFormat = $this->_config->getValue('archive_link_format'); $ownerInfo = $this->_blogInfo->getOwnerInfo(); // this is quite a dirty one but it works... $newDate = $date; if (strlen($date) == 6) { $newDate = $date . "00000000"; } if (strlen($date) == 8) { $newDate = $date . "000000"; } $t = new Timestamp($newDate); $params = array("{year}" => $t->getYear(), "{month}" => $t->getMonth(), "{blogname}" => $this->_blogInfo->getMangledBlog(), "{blogowner}" => $ownerInfo->getUsername(), "{blogid}" => $this->_blogInfo->getId()); if (strlen($date) == 6) { $params["{day}"] = ""; } else { $day = $t->getDay(); if ($day < 10) { $day = "0" . $day; } $params["{day}"] = $day; } $archiveLink = $this->getBaseUrl() . $this->_replaceTags($archiveLinkFormat, $params); return $archiveLink; }
public function testNonEpoch() { $future = '4683-03-04'; $after = new Timestamp($future); $this->assertEquals('04', $after->getDay()); $this->assertEquals('03', $after->getMonth()); $this->assertEquals('4683', $after->getYear()); $past = '1234-04-03'; $before = new Timestamp($past); $this->assertEquals('03', $before->getDay()); $this->assertEquals('04', $before->getMonth()); $this->assertEquals('1234', $before->getYear()); $this->assertFalse($after->equals($before)); $this->assertEquals($future, $after->toDate()); $this->assertEquals($past, $before->toDate()); $time = ' 00:00.00'; $this->assertEquals($future . $time, $after->toDateTime()); $this->assertEquals($past . $time, $before->toDateTime()); $past = '1-04-03'; $before = new Timestamp($past); $this->assertEquals('03', $before->getDay()); $this->assertEquals('04', $before->getMonth()); $this->assertEquals(substr(date('Y', time()), 0, 2) . '01', $before->getYear()); $past = '14-01-02'; $before = new Timestamp($past); $this->assertEquals('02', $before->getDay()); $this->assertEquals('01', $before->getMonth()); $this->assertEquals(substr(date('Y', time()), 0, 2) . '14', $before->getYear()); $past = '113-01-02'; $before = new Timestamp($past); $this->assertEquals('02', $before->getDay()); $this->assertEquals('01', $before->getMonth()); $this->assertEquals('0113', $before->getYear()); }
/** * calculates the array to display with the months * * @private */ function _getMonths() { $articles = new Articles(); $archiveStats = $articles->getNumberPostsPerMonthAdmin($this->_blogInfo->getId()); if (!$archiveStats) { return array(); } $result = array(); $t = new Timestamp(); $curyear = (int) $t->getYear(); $curmonth = (int) $t->getMonth(); $archiveDateFormat = $this->_locale->tr("archive_date_format"); if ($archiveDateFormat == "archive_date_format") { $archiveDateFormat = "%B %Y"; } foreach ($archiveStats as $yearName => $year) { foreach ($year as $monthName => $month) { // the next bit is so disgustingly ugly that I am ashamed of it... // what I'm trying to do here is that the getNumberPostsPerMonthAdmin() method // won't return the current month if there wasn't anything posted during it but // we still should show the current month even if there's nothing in it, because otherwise // when generating page where the posts are listed, it will end up saying "Date: All" // but no posts at all shown (it didn't have anything to show) This is a way of // "introducing" a month in the array without f*****g it up. In fact, PHP *was* // indeed f*****g it up... it didn't just want to keep the order! Oh well, it's a very // long and stupid story but we need this hack, ok? :) if ($archiveStats[$curyear][$curmonth] == "" && !$alreadyAdded) { // there goes the dirty hack :P if ($yearName == $curyear && $monthName < $curmonth) { $t = new Timestamp(); $name = $this->_locale->formatDate($t, $archiveDateFormat); $monthStr = array("name" => $name, "date" => $this->_locale->formatDate($t, "%Y%m")); array_push($result, $monthStr); $alreadyAdded = true; } } // we can use the Timestamp class to help us with this... $t = new Timestamp(""); $t->setYear($yearName); $t->setMonth($monthName); $name = $this->_locale->formatDate($t, $archiveDateFormat); $monthStr = array("name" => $name, "date" => $this->_locale->formatDate($t, "%Y%m")); array_push($result, $monthStr); } } return $result; }
public function testNonEpoch() { $future = '4683-03-04'; $after = new Timestamp($future); $this->assertEquals($after->getDay(), '04'); $this->assertEquals($after->getMonth(), '03'); $this->assertEquals($after->getYear(), '4683'); $past = '1234-04-03'; $before = new Timestamp($past); $this->assertEquals($before->getDay(), '03'); $this->assertEquals($before->getMonth(), '04'); $this->assertEquals($before->getYear(), '1234'); $this->assertFalse($after->equals($before)); $this->assertEquals($future, $after->toDate()); $this->assertEquals($past, $before->toDate()); $time = ' 00:00.00'; $this->assertEquals($future . $time, $after->toDateTime()); $this->assertEquals($past . $time, $before->toDateTime()); }
/** * The same as the one above but for just one month * * @param blogId The identifier of the blog from which we'd like to calculate this * @param year Year * @param month Month from which we'd like to calculate this * @return An associative array where the index is the day of the month and the value * is the number of posts made that day. */ function getNumberPostsPerDay($blogId, $year = null, $month = null) { // if month and/or year are empty, get the current ones if ($year == null) { $t = new Timestamp(); $year = $t->getYear(); } if ($month == null) { $t = new Timestamp(); $month = $t->getMonth(); } // another long sql query :) the id and date fields are there just in case we need to debug // but they're not included in the resulting array // $blogs = new Blogs(); $blogSettings = $this->blogs->getBlogSettings($blogId); // consider the time difference $timeDifference = $blogSettings->getValue("time_offset"); $SecondsDiff = $timeDifference * 3600; // check whether we're supposed to show posts that happen in the future or not if ($blogSettings->getValue("show_future_posts_in_calendar")) { $numPostsPerDayQuery = "SELECT COUNT(*) AS 'count', DAYOFMONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff})) AS 'day', id AS 'id', date AS 'date' FROM " . $this->getPrefix() . "articles WHERE status = 1 AND blog_id = " . $blogId . " AND MONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff})) = " . $month . " AND YEAR(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff})) = " . $year . " GROUP BY DAYOFMONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff}));"; } else { $numPostsPerDayQuery = "SELECT COUNT(*) AS 'count', DAYOFMONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff})) AS 'day', id AS 'id', date AS 'date' FROM " . $this->getPrefix() . "articles WHERE status = 1 AND blog_id = " . $blogId . " AND date <= NOW() AND MONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff})) = " . $month . " AND YEAR(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff})) = " . $year . " GROUP BY DAYOFMONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(date) + {$SecondsDiff}));"; } $result = $this->Execute($numPostsPerDayQuery); if ($result == false) { return 0; } $postsPerDay = array(); while ($row = $result->FetchRow()) { $postsPerDay[$row["day"]] = $row["count"]; } $result->Close(); return $postsPerDay; }
/** * Fetches the stats for the archives * * @private */ function _getArchives() { $archiveStats = $this->articles->getNumberPostsPerMonth($this->_blogInfo->getId()); if ($archiveStats == '') { return false; } $links = array(); $locale = $this->_blogInfo->getLocale(); $urls = $this->_blogInfo->getBlogRequestGenerator(); // format of dates used in the archive, but it defaults to '%B %Y' if none specified $archiveDateFormat = $locale->tr('archive_date_format'); // need to check whether we got the same thing back, since that's the way Locale::tr() works instead of // returning an empty string if ($archiveDateFormat == "archive_date_format") { $archiveDateFormat = ARCHIVE_DEFAULT_DATE_FORMAT; } foreach ($archiveStats as $yearName => $year) { foreach ($year as $monthName => $month) { // we can use the Timestamp class to help us with this... $t = new Timestamp(); $t->setYear($yearName); $t->setMonth($monthName); $archiveUrl = $urls->getArchiveLink($t->getYear() . $t->getMonth()); $linkName = $locale->formatDate($t, $archiveDateFormat); $link = new ArchiveLink($linkName, '', $archiveUrl, $this->_blogInfo->getId(), 0, $month, 0); $links[] = $link; } } return $links; }
/** * Checks if there is at least a year and a month in the request */ function checkDateParameter() { $date = $this->_request->getValue('Date'); if ($date) { $year = substr($date, 0, 4); $month = substr($date, 4, 2); $day = substr($date, 6, 2); } else { $t = new Timestamp(); $year = $t->getYear(); $month = $t->getMonth(); $day = $t->getDay(); } $this->_session->setValue('Year', $year); $this->_session->setValue('Month', $month); $this->_session->setValue('Day', $day); }