Skip to main content

๐Ÿ Conversion

zjkalAbout 1 min

๐Ÿ Conversion

Tips

It is used to convert crawlers crawling web pages or third-party programs to connect with different time formats

format()

Convert any format time to the specified format

Parameters

ParameterIntroductionRequire
formatThe date format is the same as that of the system function date().Optional, default is Y-m-d H:i:s
datetimeAny type of string time or timestampOptional, defaults to the current time

Return Values

Specifies the format of the time string

Example code

/* Convert the following dates to a date format commonly used in China */
TimeHelper::format('Y-m-d H:i:s','May 3, 2022 12:01:33');
// 2022-03-03 12:01:33

/* Convert timestamps to date formats commonly used in the West */
TimeHelper::format('F j, Y, g:i a',1698762509);
// October 31, 2023, 10:28 pm

isTimestamp()

Determines whether a string or number is a timestamp

Parameters

ParameterIntroductionRequire
timestampThe string or number to be judgedrequired

Return Values

'true' if it's a timestamp, 'false' if it's otherwise

Example code

/* Check whether the following strings are timestamps */
TimeHelper::isTimestamp('1646360133');
// true

/* Determine whether the following numbers are timestamps */
TimeHelper::isTimestamp(164636013);
// false

toTimestamp()

Arbitrary format time is converted to a timestamp

Parameters

ParameterIntroductionRequire
datetimeAny type of string time or timestampOptional, defaults to the current time

Return Values

The timestamp after the conversion

Example code

/* Convert the following dates to timestamps */
TimeHelper::toTimestamp('Apr 11, 2020');
// 1586534400

/* Convert irregular dates to timestamps */
TimeHelper::toTimestamp('2023ๅนด9ๆœˆ 5็‚น');
// 1693515600

modifyTimestamp()

Modify the specified time based on the date string

Parameters

ParameterIntroductionRequire
modifierDate modification stringRequired
datetimeAny type of string time or timestampOptional, defaults to the current time

Return Values

Modified date and time stamp

Example code

/* Add one day to the specified date */
TimeHelper::modifyTimestamp('+1 day', '2023-9-1 12:00:00');
// 1693627200

now()

Get the current time

Parameters

ParameterIntroductionRequire
formatThe date format is the same as that of the system function date().Optional, default is Y-m-d H:i:s

Return Values

Specifies the current time of the format

Example code

/* `Get the current time` */
TimeHelper::now();
// 2024ๅนด7ๆœˆ2ๆ—ฅ 10:01:33

/* Gets the current time in the specified format */
TimeHelper::now('M d, Y H:i:s');
// July 2, 2024 10:01:33