search
HomeBackend DevelopmentPHP TutorialPHP variable and date processing cases

PHP variable and date processing cases

Jun 07, 2018 pm 04:35 PM
phpvariableDate processing

This article mainly introduces PHP variables and date processing cases. Interested friends can refer to it. I hope it will be helpful to everyone.

Variable related
Internal implementation of PHP variables
The system types of programming languages ​​are divided into two types: strong type and weak type:

  1. Strong Type language means that once a variable is declared as a variable of a certain type, a value other than the type of the variable cannot be assigned to it during the running of the program. Languages ​​such as c/c/java fall into this category

  2. Scripting languages ​​such as php, ruby, and javascript are weakly typed languages: a variable can represent any data type


php Variable types and storage structuresWhen php declares or uses variables, it does not need to explicitly indicate its data type

php is a weakly typed language. This does not mean that php has no type. In php, There are 8 types of variables, which can be divided into three categories:

  1. Scalar type: boolean, integer, float, string

  2. Composite type: array ,object

  3. ## Special type: resource,NULL


Variable storage structureThe value of the variable is stored in In the zval structure shown. Its structure is as follows:

  typedef struct _zval_struct zval; 
   
  struct _zval_struct { 
    zvalue_value value; // 存储变量的值 
    zend_uint refcount__gc; // 表示引用计数 
    zend_uchar type;  // 变量具体的类型 
    zend_uchar is_ref_gc;  // 表示是否为引用 
  };

The value of the variable is stored in another structure zvalue_value

Variable typezval structure The type field is the most critical field to implement weak types. The value of type can be one of: IS_NULL, IS_BOOL, IS_LONG, IS_DOUBLE, IS_STRING, IS_ARRAY, IS_OBJECT, IS_RESOURCE. It is easy to understand literally, they are only the only types of Mark, store different values ​​in the value field according to different types

Storage of variable valuesAs mentioned earlier, the value of the variable is stored in the zvalue_value structure, and the structure is defined as follows:

  typedef union _zvalue_value { 
    long lval; 
    double dval; 
    struct { 
      char *val; 
      int len; 
    } str; 
    HashTable *ht; 
    zend_object_value obj; 
  } _zvalue_value;


Date related

Calculate the number of days between two dates


 <?php 
   
  /** 
   * 求两个日期之间相差的天数(针对1970年1月1日之后,求之前可以采用泰勒公式) 
   * @param string $day1 
   * @param string $day2 
   * @return number 
   */ 
  function diffBetweenTwoDays ($day1, $day2) 
  { 
    $second1 = strtotime($day1); 
    $second2 = strtotime($day2); 
     
    if ($second1 < $second2) { 
      $tmp = $second2; 
      $second2 = $second1; 
      $second1 = $tmp; 
    } 
     
    return ($second1 - $second2) / 86400; 
  } 
   
  $day1 = "2013-07-27"; 
  $day2 = "2013-08-04"; 
   
  $diff = diffBetweenTwoDays($day1, $day2); 
  echo $diff."\n";

Summary:That’s it The entire content of this article is hoped to be helpful to everyone's study.

Related recommendations:

Detailed explanation of DS extension of data structure in PHP

PHP implementation of extracting strings Mobile phone number regular expression method

PHP implementation method of generating and parsing xml based on SimpleXML

The above is the detailed content of PHP variable and date processing cases. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

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),