devwiki:php

PHP and SQL

  • select today data entry
    where DATE(`time_entry`) = CURDATE()
  • select current week data entry with option of week start day
    where YEARWEEK(`time_entry`) = YEARWEEK(CURDATE())
    where YEARWEEK(`time_entry`, 1) = YEARWEEK(CURDATE(), 1)
    • Note: weekofyear return week number, means previous year will return too
      where  WEEKOFYEAR(`time_entry`) = WEEKOFYEAR(NOW());
  • select current month data entry
    where YEAR(`time_entry`) = YEAR(NOW()) AND MONTH(`time_entry`)=MONTH(NOW());
  • select past 24 hr
    where `time_entry`> DATE_SUB(NOW(), INTERVAL 1 DAY)
  • select past 7 days
    where `time_entry`> DATE_SUB(NOW(), INTERVAL 1 WEEK)
  • select past 1 month time
    where `time_entry`> DATE_SUB(NOW(), INTERVAL 1 MONTH)
  • array related
    // sort array
    sort($my_list);

Php Date and Time Related

  • all time info
    date('N'); // current day of week, 6 for saturday
    date('N', date_val) // day of week for that date data
    date("Y-m-d"); // today's date string
  • get current week range (monday to sunday) or any day's week range
    //ref: https://stackoverflow.com/questions/5552862/finding-date-range-for-current-week-month-and-year
    function rangeWeek($datestr) {
        date_default_timezone_set('Asia/Singapore');
        $date_val = strtotime($datestr);
        return array (
         "start" => date('N', $date_val) == 1 ? date ('Y-m-d', $date_val) : date ('Y-m-d', strtotime('last monday', $date_val)),
         "end" => date('N', $date_val) == 7 ? date ('Y-m-d', $date_val) : date ('Y-m-d', strtotime('next sunday', $date_val))
        );
    }
    echo rangeWeek('2011-4-5');
  • devwiki/php.txt
  • Last modified: 2021/08/28 17:45
  • by ying