PHP calculates the time difference between two dates (returns year, month, day)

In the PHP program, you will often encounter processing time problems, such as: judging how long the user has been online, how many days have been logged in, the time difference between two posts, or log records between different operations, etc. wait. In the article, a simple example of how to calculate the difference between two dates in PHP is year, month, and day. strtotime($date2)) { $ymd = $date2; $date2 = $date1; $date1 = $ymd; } list($y1, $m1, $d1) = explode(‘-‘, $date1); list($y2, $m2, $d2) = explode(‘-‘, $date2); $y = $m = $d = $_m = 0; $math = ($y2 – $y1) * 12 + $m2 – $m1; $y = round($math / 12); $m = intval($math % 12); $d = (mktime(0, 0, 0, $m2, $d2, $y2) – mktime(0, 0, 0, $m2, $d1, $y2)) / 86400; if ($d <0) { $m -= 1; $d += date('j', mktime(0, 0, 0, $m2, 0, $y2)); } $m

PHP calculates age accurately to year, month, day

The example in this article describes how PHP calculates the age accurately to the year, month, and day. Share it with everyone for your reference. The details are as follows: <?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ class Age { /** * The calculated age is accurate to the year, month, and day * @param type $birthday * @return array */ public function calAge($birthday) { list($byear, $bmonth, $bday) = explode('-', $birthday); list($year, $month, $day) = explode('-', date('Y-m-d')); $bmOnth = intval($bmonth); $bday = intval($bday); if ($bmonth <10) { $bmOnth = '0' . $bmonth; } if ($bday $ni) { $not_birth = 1; $tmp = array($byear, $bmonth, $bday); list($byear, $bmonth, $bday) = array($year, $month, $day); list($year, $month, $day) = $tmp; list($bi, $ni) = array($ni, $bi); } $years = 0; while (($bi + 10000) getMD(array($year, $month, $day), array($byear, $bmonth, $bday)); return array(‘year’ => $years, ‘month’ => $m, ‘day’ => $d, ‘not_birth’ => $not_birth); } /** * Can only be used for calculation within one year * @param type $ymd * @param type $bymd */ public function getMD($ymd, $bymd)…

The php page sends three int year, month, day, how to convert the three numbers into a date format

Three int-type year, month and day come from the php page, how to convert the three numbers into a date format Tips: 1. In the PHP language, the requirements for different data types are not very strict. For example, ‘122.22’ can be used not only as a string, but also as a value for addition and subtraction. 2. For your question, the first thing to remind is that it is pointed out from the PHP manual: “PHP supports 8 primitive data types”, and among these 8 types, there is no clear “date” type. How does php change the timestamp into a date php changes the timestamp into a date, the tool used, notepad++, the steps are as follows: php code part: <?php$t=time();echo "Today's date and timestamp is: ".$t."“;echo “Convert timestamp to date: ” .date(“Y-m-d H:i: s”, $t);?> Description: Get the timestamp of the current date first, and then use the data function to convert the timestamp into a date. $t can be any timestamp. Effect picture after running: Note: the code must run in the php environment. How to convert the time format of year, month, day, hour, minute, and second into year, month, and day in the PHP program,…

PHPDateTime difference (hour, day, week, month)

I’m creating a PHP function that will return the difference between two dates in two formats: 2 months, 3 weeks, 6 days, 3 hours. I tried using the PHP DateTime class, but it only returns Months, Days and Hours, and I can’t find a way to calculate Weeks. Here is my function: public function DateTimeDifference($FromDate, $ToDate) { $FromDate = new DateTime($FromDate); $ToDate = new DateTime($ToDate); $Interval = $FromDate->diff($ToDate); $Difference[“Hours”] = $Interval->h; $Difference[“Days”] = $Interval->d; $Difference[“Months”] = $Interval->m; return $Difference; } Now, I need $Difference[“Weeks”] to also be included in the return data. EDIT: I know I can divide Days by 7 and get weeks, but the result is incorrect. Example: 2 months, 14 days, 3 hours – when I divide 14 days by 7, I get: 2 months, 2 weeks, 14 days, 3 hours, now this is not the same period. 1> iXCray..: public function DateTimeDifference($FromDate, $ToDate) { $FromDate = new DateTime($FromDate); $ToDate = new DateTime($ToDate); $Interval = $FromDate->diff($ToDate); $Difference[“Hours”] = $Interval->h; $Difference[“Weeks”] = floor($Interval->d/7); $Difference[“Days”] = $Interval->d % 7; $Difference[“Months”] = $Interval->m; return $Difference; }

Is there a way for mysql-Php to compare whether the year, month, and day of two times are equal?

Article directory [hide] Reply content: My database may contain time stamps of hours, minutes and seconds , now there is a time when only the year, month, and day need to be compared and if they are equal, they will be queried. Can anyone guide how to judge the query, thank you very much. Reply content: My data inventory may contain time stamps of hours, minutes, and seconds. Now there is a time that only needs to be compared with the year, month, and day. If they are equal, I will query them. Can any master guide how to judge the query, thank you very much. There are still many methods, but I think you need fuzzy query, so I will talk about one that is easier to understand1. Set only the time of “year-month-day” Use the sorttime function to convert it into a timestamp, so you can get time1 at 0:00 of the day;2. Still use the sorttime function to get +1 time, or directly add the total seconds of 1 day, the variable name time2, so there will be 2 books and 2 texts from *source gao($daima.com @代@#码(网engage gaodaima code time nodes3. Use between query , so that…

PHP gets the year, month, and day between the specified time periods  , method of hour, minute and second

PHP gets the year, month, day, hour, minute, and second between the specified time period

Backend Development | PHP Tutorial php, time period, specify the back-end development-php tutorial front-end pass two standards Come in the time format, the format is like 2009-05-12 12:12:30, and then return the representation of different units of this time period according to the needs. For the verification of the time format, I have not posted the code here, so think about it when you use it Add the source code of Zhonghuakang.com, clear the inner and outer margins of vscode, check tomcat memory in opt, jsp in ubuntu, home crawler pictures, php rps, docking with outsourcing companies seolzw core code: picture php source code, import pictures in vscode, ubuntu is backup, how to check tomcat address, 360 crawler ua, php mail client, Xi’an professional whole site optimization seo price, franchise website program source code, bank front desk template free download lzw Class Utils { /** * format MySQL DateTime ( YYYY-MM-DD hh:mm:ss) Convert the data format found in mysql into time seconds * @param string $datetime */ public function fmDatetime($datetime) { $year = substr($datetime,0, 4); $mOnth = substr($datetime,5,2); $day = substr($datetime,8,2); $hour = substr($datetime,11,2); $min = substr($datetime, 14,2); $sec = substr($datetime,17,2); return mktime($hour,$min,$sec,$month,$day,0+$year); } /** * * According to two…

How to convert PHP year, month, day to timestamp

How to convert php year, month, day to timestamp: first create a PHP sample file; then define a year, month, day and time; finally pass “mktime(0,0,0,$arr['month&#039 ;], $arr['day'], $arr['year']);” to convert the year, month, day into a timestamp. The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer PHP specified date to time stamp Use date_parse_from_format to convert the specified format: For example: <?php $str = '2018.10.01'; // or October 1, 2018 $arr = date_parse_from_format('Y.m.d', $str);//If it is October 1, 2018, then here is Y year m month d day $time = mktime(0,0,0,$arr['month'],$arr['day'],$arr['year']) ; print_r($arr); echo 'The corresponding timestamp is: '.$time; Output: Array ( [year] => 2018 [month] => 10 [day] => 1 [hour] => [minute] => [second] => [fraction] => [ warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => ) The corresponding timestamp is: 1538352000 mktime() definition and usage The gmmktime() function returns the UNIX timestamp of a date. Tip: This function is the same as gmmktime(), except that the passed argument represents a date (rather than a GMT date). [Recommended learning: “PHP Video Tutorial”] Syntax mktime(hour,minute,second,month,day,year,is_dst); year is optional. specified year. parameter Description hour Optional.…

PHP calculates age accurately to year, month, day_PHP

The example in this article describes how PHP calculates the age accurately to the year, month, and day. Share it with everyone for your reference. The details are as follows: <?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor . */ class Age { /** * Calculate age accurately to year, month, day* @param type $birthday * @return array */ public function calAge($birthday) { list($byear, $bmonth, $bday) = explode ('-', $birthday); list($year, $month, $day) = explode('-', date('Y-m-d&#039 ;)); $bmOnth= intval($bmonth); $bday = intval($bday); if ($bmonth $years, 'month' => $m , 'day' => $d, 'not_birth' => $not_birth); } /** * Can only be used for calculation within one year * @param type $ymd * @param type $bymd */ public function getMD($ymd, $bymd) { list($y, $m, $d) = $ymd; list($by, $bm, $bd) = $bymd; if (($m . $d) <($bm . This article comes from *source gaodai^.ma#com商#code! Code Networkengage gaodaima code $bd)) { $m +=12; } $mOnth= 0; while ((($bm . $bd) + 100) _getMothDay( ++$by, $bm – 12) : $this->_getMothDay($by, $bm); $day = $mdays – $bd + $d; }…

PHP knows a specific year, month, day and time, and calculates the time after adding a tenth second, how to achieve it?

For example: 123456 $date1 = “2016-11-25 09:53:58”; $p_time = “00:02:34”; // $date1 + $p_time Result output: 1 “2016-11-25 09:56:32”

PHP uses regular expressions to verify Chinese year, month, and day

How does PHP verify that the user enters a Chinese “year” and “month” date, such as “January 2017 record”. After verifying this format, how to extract the numbers “2017” and “1”?

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索