Home Backend Development PHP Tutorial Convert date to timestamp in php, convert specified date to timestamp

Convert date to timestamp in php, convert specified date to timestamp

Jun 06, 2018 am 11:29 AM
date Timestamp

This article mainly introduces about converting php date to timestamp, converting a specified date into a timestamp, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

UNIX timestamp Formatted dates and formatted dates are two time representations that we often deal with. Unix timestamps are easy to store and process, but not intuitive. Formatted dates are intuitive, but not as easy to process as Unix timestamps, so sometimes they need to be converted to each other. The following are several conversion methods for converting PHP date to timestamp and MySQL date conversion functions to each other

Programmers who have written PHP MySQL all know that there is a time difference. UNIX timestamps and formatted dates are what we often deal with. Two time representations, Unix timestamp is convenient to store and process, but not intuitive. Formatted date is intuitive, but it is not as easy to process as Unix timestamp, so sometimes it is necessary to convert each other. Here are several conversions for each other. Way.
1. Complete in MySQL  
This method is converted in the MySQL query statement. The advantage is that it does not take up the parsing time of the PHP parser and is fast. The disadvantage is that it can only be used for database queries. , there are limitations.
1. Use the function to convert UNIX timestamp to date: FROM_UNIXTIME()
General form: select FROM_UNIXTIME(1156219870);
2. Use the function to convert date to UNIX timestamp: UNIX_TIMESTAMP()
General Format: Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
Example: mysql query the number of records on the day:
$sql="select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime), '%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc";
Of course, you can also choose to convert in PHP, as follows Let’s talk about conversion in PHP.
2. Complete in PHP  
This method completes the conversion in the PHP program. The advantage is that it can be converted regardless of whether the data obtained by querying in the database is, and the conversion range is not limited. The disadvantage is that It takes up the parsing time of the PHP parser and is relatively slow.
1. Use the function to convert UNIX timestamp to date: date()
General form: date('Y-m-d H:i:s', 1156219870);
2. Use the function to convert date to UNIX timestamp :strtotime()
General form: strtotime('2010-03-24 08:15:42');
php date to timestamp, the specified date is converted to a timestamp
PHP date to timestamp, specified date to timestamp, PHP scheduled tasks.
In the past two days, we need to implement this function:
When a certain condition is reached, let the server send multiple text messages to the user.
Basic idea: Linux scans regularly, and if there are users who meet the conditions, send text messages.
However, in order to prevent disturbing users, it is required that text messages can only be sent between 8:00-20:00 during the day. How to obtain this time range every day?
The following code:

Copy code The code is as follows:

<? php
$y=date("Y",time()); 
$m=date("m",time()); 
$d=date("d",time()); 
$start_time = mktime(9, 0, 0, $m, $d ,$y); 
$end_time = mktime(19, 0, 0, $m, $d ,$y); 
$time = time(); 
if($time >= $start_time && $time <= $end_time) 
{ 
// do something.... 
} 
?>
Copy after login

Related recommendations:

php Common date phase functions [Date addition and subtraction, difference between two dates, date conversion time cutoff]

PHP date and time, PHP date time


The above is the detailed content of Convert date to timestamp in php, convert specified date to timestamp. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

1. First open the mobile web browser, search for the Weibo web version, and click the avatar button in the upper left corner after entering. 2. Then click Settings in the upper right corner. 3. Click the version switching option in settings. 4. Then select the color version option in the version switch. 5. Click Search to enter the search page. 6. After entering the keywords, click Find People. 7. When the search completion interface appears, click Filter. 8. Finally, enter the specific date in the release time column and click Filter.

How to remove the date that appears automatically when printing from PPT handouts How to remove the date that appears automatically when printing from PPT handouts Mar 26, 2024 pm 08:16 PM

1. Let me first talk about the method I used at the beginning, maybe everyone is using it too. First, open [View]——]Remarks Template[. 2. A place where you can actually see the date after opening it. 3. Select it first and delete it. 4. After deleting, click [Close Master View]. 5. Open the print preview again and find that the date is still there. 6. In fact, this date was not deleted here. It should be in the [Handout Master]. Look at the picture below. 7. Delete the date after you find it. 8. Now when you open the preview and take a look, the date is no longer there. Note: In fact, this method is also very easy to remember, because the printed handouts are handouts, so you should look for the [Handout Master].

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

How to generate k random dates between two dates using Python? How to generate k random dates between two dates using Python? Sep 09, 2023 pm 08:17 PM

Generating random data is very important in the field of data science. From building neural network predictions, stock market data, etc., date is usually used as one of the parameters. We may need to generate random numbers between two dates for statistical analysis. This article will show how to generate k random dates between two given dates using the random and datetime modules. Datetime is Python’s built-in library for handling time. On the other hand, the random module helps in generating random numbers. So we can combine random and datetime modules to generate a random date between two dates. Syntax random.randint (start, end, k) random here refers to the Python random library. The randint method uses three important

How to display date and seconds in the top bar of Ubuntu 17.10? How to display date and seconds in the top bar of Ubuntu 17.10? Jan 08, 2024 am 10:41 AM

By default, the top bar of Ubuntu 17.10 only has the current time and no date. What should I do if I want to display the date? Let’s take a look at the detailed tutorial below. 1. Open the terminal in the launcher, or press [Ctrl+Alt+T] 2. Enter in the terminal: sudoaptinstallgnome-tweak-tool 3. After the installation is completed, open the tweak tool 4. Click TopBar 5. Date is the date and seconds is the number of seconds 6. After setting it up, the date and seconds will be displayed on the time in the top bar.

Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class Java documentation interpretation: Usage analysis of the currentTimeMillis() method of the System class Nov 03, 2023 am 09:30 AM

Java document interpretation: Usage analysis of the currentTimeMillis() method of the System class, specific code examples are required. In Java programming, the System class is a very important class, which encapsulates some properties and operations related to the system. Among them, the currentTimeMillis method is a very commonly used method in the System class. This article will explain the method in detail and provide code examples. 1. Overview of currentTimeMillis method

How to change the date into a pound sign in Excel How to change the date into a pound sign in Excel Mar 20, 2024 am 11:46 AM

Excel software has very powerful data processing functions. We often use excel software to process various data. Sometimes when we enter a date in an excel cell, the date in excel changes to a pound sign. How can we display the data normally? Let’s take a look at the solution below. 1. First, we put the mouse on the column width line between columns AB, double-click and adjust the column width, as shown in the figure below. 2. After the column is widened, we find that numbers are displayed in the cells instead of dates. This is definitely incorrect. Then we should check the format of the cells, as shown in the figure below. 3. Click the &quot;Number&quot; option in the &quot;Home&quot; tab, and click &quot;Other Number Format&quot; in the drop-down menu, as shown in the figure below.

How to use the time and date modules in Python How to use the time and date modules in Python Oct 16, 2023 am 08:11 AM

How to use the time and date modules in Python Introduction: In programming, dealing with time and dates are very common tasks. Python provides powerful time and date modules, making time and date operations easier and more convenient. This article will introduce the time and date modules in Python and provide specific code examples to help readers better understand and apply them. 1. Introducing the time and date module Python’s built-in time and date module is the datetime module. We need to introduce this module first.

See all articles