Hongmu Notes
Home Language Notes A PHP function that adds or subtracts a specified amount of time from a timestamp and returns the result in a specified format.
Language Notes PHP PHP and mysql

A PHP function that adds or subtracts a specified amount of time from a timestamp and returns the result in a specified format.

A PHP function that adds or subtracts a specified amount of time from a timestamp and returns the result in a specified format.

Here is a PHP function that adds or subtracts a specified amount of time from a timestamp and then outputs the resulting time in a specified format:

<?php
/**
 * 将指定时间与时间戳相加减后,输出指定格式时间
 *
 * @param string $time 日期时间字符串,格式为 'Y-m-d H:i:s'
 * @param int $seconds 要添加或减去的秒数,正数为加,负数为减
 * @param string $format 输出的日期时间格式,默认为 'Y-m-d H:i:s'
 * @return string 格式化后的日期时间字符串
 */
function add_seconds_to_time($time, $seconds, $format = 'Y-m-d H:i:s') {
    // 将日期时间字符串转换为时间戳
    $timestamp = strtotime($time);

    // 计算加减后的时间戳
    $new_timestamp = $timestamp + $seconds;

    // 将时间戳格式化为指定格式的日期时间字符串
    $new_time = date($format, $new_timestamp);

    // 返回格式化后的日期时间字符串
    return $new_time;
}

// 示例用法
$time = '2023-04-25 13:03:02';
$seconds = 3600; // 假设要将时间加上1小时
$new_time = add_seconds_to_time($time, $seconds);
echo $new_time; // 输出 '2023-04-25 14:03:02'
?>

You can save the above code as a PHP file and call this function wherever needed to add or subtract a specified amount of time from or to a timestamp. When calling this function, you must pass three parameters:

  • $timeDate and time string in the format 'Y-m-d H:i:s'
  • $secondsThe number of seconds to add or subtract; a positive value adds seconds, while a negative value subtracts seconds.
  • $formatThe output date and time format defaults to 'Y-m-d H:i:s'; you can specify a custom format as needed.

The function returns a formatted date and time string.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 145

Recommended reading

(Adaptive Mobile Version) Advertising Agency Website Template – Download Source Code for Advertising Design Agency Websites – 1051

(Adaptive Mobile Version) Advertising Agency Website Template – Download Source Code for Advertising Design Agency Websites – 1051 Practical Collection pbootcms Template

A PbootCMS website template designed for advertising and cultural agencies as well as advertising design firms, supporting mobile access. Its creative and visually appealing design makes it ideal for these agencies to showcase their brand strategy, design works, and case studies. This template helps advertising agencies demonstrate their professional expertise online and attract brand clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 66
(Adaptive mobile version) Industrial wastewater treatment equipment – PBootCMS website template; Environmental Protection Technology website source code download – 0340

(Adaptive mobile version) Industrial wastewater treatment equipment – PBootCMS website template; Environmental Protection Technology website source code download – 0340 Practical Collection pbootcms Template

This set includes adaptive mobile-friendly industrial wastewater treatment equipment and an eco-friendly technology PbootCMS website template. The professional, tech-driven design is ideal for showcasing wastewater treatment technologies, equipment products, and project cases. It enables environmental engineering companies to present their solutions online and attract industrial enterprise clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 54
Assign the result of a multiple-selection form submission to a hidden form in EmpireCMS.

Assign the result of a multiple-selection form submission to a hidden form in EmpireCMS. Program Notes Empire cms

In EmpireCMS, if you need to implement a multiple-selection feature—such as a multi-region search—you must concatenate all the forms generated after the multi-region selection and then pass them to the `keyboard` field. For this purpose, the following code can be used: HTML code snippet: <label class="area-box-listhover"> <input class="inputcheckb...>
👁 165
Centrifugal Fan Mechanical Manufacturing Website Template 0608

Centrifugal Fan Mechanical Manufacturing Website Template 0608 Practical Collection Yiyou template

This EyouCMS template is ideal for the centrifugal fan and mechanical manufacturing industries, featuring a professional industrial design style that effectively showcases centrifugal fan products, technical specifications, and industrial applications. It helps fan equipment manufacturers demonstrate their product capabilities online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 57
(Adaptive mobile version) Furniture manufacturing company website template – PBootCMS furniture website source code download (1052)

(Adaptive mobile version) Furniture manufacturing company website template – PBootCMS furniture website source code download (1052) Practical Collection pbootcms Template

A PbootCMS website template designed for furniture manufacturing companies, compatible with both PC and WAP devices. Its modern and practical design is ideal for showcasing furniture products, production capabilities, and brand stories. This template helps furniture businesses present their brands online and attract distributors and consumers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pbo...
👁 57