Home  >  Article  >  Backend Development  >  How to remove hours, minutes and seconds from time in php

How to remove hours, minutes and seconds from time in php

藏色散人
藏色散人Original
2023-03-13 11:20:483188browse

php method to remove hours, minutes and seconds from time: 1. Create a php sample file; 2. Use the strtotime function to convert the date and time into a timestamp; 3. Format the date or time through the date function. Hours, minutes and seconds can be removed.

How to remove hours, minutes and seconds from time in php

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, Dell G3 computer.

php How to remove hours, minutes and seconds from time?

It's very simple. You can use the "e35cc10428f723ef0ab8bbc6da772289" method to remove the hours, minutes and seconds, leaving only the year, month and day.

  • The date() function in PHP is used to format date or time.

PHP The Date() function formats a timestamp into a more readable date and time.

date(format,timestamp)

Parameter description

format required. Specifies the format of the timestamp.

timestamp Optional. Specify timestamp. The default is the current time and date.

Note: A timestamp is a sequence of characters that represents the date and event when a specific event occurred.

Get a simple date

The format parameters of the date() function are required, and they specify how to format the date or time.

Here are some characters commonly used for dates:

  • d - represents the day of the month (01-31)

  • m - represents the month (01-12)

  • Y - represents the year (four digits)

  • 1 - represents the week

Other characters, such as "/", "." or "-" can also be inserted into the characters to add other formatting.

The following example formats today's date in three different ways:

Example

<?php
echo "今天是 " . date("Y/m/d") . "<br>";
echo "今天是 " . date("Y.m.d") . "<br>";
echo "今天是 " . date("Y-m-d") . "<br>";
echo "今天是 " . date("l");
?>
  • strtotime function: Describe the date and time of any string Parse to Unix timestamp:

strtotime() function parses any string datetime description into a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT ).

Note: If the year representation uses a two-digit format, values ​​0-69 will be mapped to 2000-2069, and values ​​70-100 will be mapped to 1970-2000.

Note: Please note that for dates in m/d/y or d-m-y format, if the separator is a slash (/), the American m/d/y format is used. If the delimiter is a dash (-) or a dot (.), the European d-m-y format is used. To avoid potential errors, you should use YYYY-MM-DD format whenever possible or use the date_create_from_format() function.

Syntax

int strtotime ( string $time [, int $now = time() ] )

Parameters

time is required. Specifies a date/time string.

nowOptional. Specifies the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to remove hours, minutes and seconds from time in php. 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