I have gained a lot after working for a year. I will slowly make some summaries to improve work efficiency.
The time used by mysql at work is a UNIX timestamp: the number of seconds from 0:00 on January 1, 1970 to the current time. Since it is an int type, it is very convenient for computer processing, not just PHP and MySQL is a format for data interaction. It is also the standard for data interaction in various clients (android/IOS), etc. Therefore, if you only save and display dates, you should use UNIX timestamps to calculate dates and as standard Date format.
The commonly used process in work is: convert the time of the HTML page into a timestamp and save it in mysql, and then take the timestamp from mysql and format it for display on the web or mobile client. In short, the time saved in mysql is a UNIX timestamp, which is then formatted into a suitable time by PHP
Introducing several commonly used functions
1.date(),2.mktime(),3.getdate(),4.strftime()
1.date()
Get time and date in PHP
Use the date() function: convert the timestamp or current time into a formatted string, for example:
echo date('Y-i-s');//Output 2014-3-25
2.mktime()
Use mktime() to convert time into UNIX timestamp
$timestamp = mktime();
There are three ways to get the current timestamp:
mktime(),time(),date('U')
mktime does time calculations
mktime(12,0,0,$mon,$day+10,$year); timestamp ten days later
3.getdate() function:
$today = getdate();
print_r($today);
//Output
Array
(
[seconds] => 38
[minutes] => 38
[hours] => 22
[mday] => 25
[wday] => 2
[mon] => 3
[year] => 2014
[yday] => 83
[weekday] => Tuesday
[month] => March
[0] => 1395758318
)
Use checkdate() function to check date validity
4.strftime()
Format timestamp
mysql formatting time
1.DATE_FORMAT()
2.UNIX_TIMESTAMP() returns the date formatted as a UNIX timestamp, for example: SELECT UNIX_TIMESTAMP(date) FROM table, so it can be processed in PHP
There are relatively few functions for formatting time in PHP. Here are some commonly used formatting time functions
/** * *将timestamp时间转化为x时x分x秒 * */ public static function getTimeLong($seconds) { if (!$seconds) { return '0秒'; } $ret = ''; if ($seconds >= 3600) { $hours = (int)($seconds / 3600); $seconds = $seconds % 3600; if ($hours) { $ret .= ($hours . '时'); } } if ($seconds >= 60) { $mi = (int)($seconds / 60); $seconds = $seconds % 60; if ($mi) { $ret .= ($mi . '分'); } } if ($seconds) { $ret .= ($seconds . '秒'); } return $ret; }
/** * 将相差timestamp转为如“1分钟前”,“3天前”等形式 * * @param timestamp $ts_diff 当前时间 - 要格式化的timestamp */ public static function formatTime($ts_diff) { if ($ts_diff <=0) { return date('Y-m-d'); } else if ( $ts_diff <= 3600 ) { return max(1, (int)($ts_diff/60)) . '分钟前'; } else if ( $ts_diff <= 86400 ) { return ((int)($ts_diff/3600)) . '小时前'; } else { return ((int)($ts_diff/86400)) . '天前'; } }
/** 将数字星期转换成字符串星期 weekNum2String($num) * @param int * @return string */ public static function weekNum2String($num){ switch($num){ case 1: return '星期一'; case 2: return '星期二'; case 3: return '星期三'; case 4: return '星期四'; case 5: return '星期五'; case 6: return '星期六'; case 7: return '星期日'; default: return '未知'; } }

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
