Convert hour to timestamp php

IMPORTANT:

In addition to above answers ,there is an important thing that one must follow.Always use the With() function(see below) i.e

Always use :

$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone)); 

Do not use:

$newTimezone = new DateTime($day, new DateTimeZone($timezone));

REASON:(different outputs, check below)

function with($day,$timezone){
    $newTimezone = new DateTime($day);
    $newTimezone->setTimezone(new DateTimeZone($timezone)); 
    $timestamp = $newTimezone->format('U');
    return $timestamp;

}    
function without($day,$timezone){
    $newTimezone = new DateTime($day, new DateTimeZone($timezone));
    $timestamp = $newTimezone->format('U');
    return $timestamp;
}   
     
$tomorrow = date('Y-m-d h:i:s A', strtotime('-1 seconds ' ,strtotime('tomorrow midnight')));
$yesterday = date('Y-m-d h:i:s A', strtotime('+24 hours 1 seconds ' , strtotime('yesterday midnight')));
$timezone = 'UTC';
        


echo 'With Yesterday: '.with($yesterday,$timezone).'
'; $now = new DateTime('@'.with($yesterday,$timezone)); $now->setTimezone(new DateTimeZone(date_default_timezone_get())); echo 'With Yesterday Readable: '.$now->format('m/d/Y h:i:s A').' -------- '.date('m/d/Y h:i:s A').'

'; echo 'Without Yesterday: '.without($yesterday,$timezone).'
'; $now = new DateTime('@'.without($yesterday,$timezone)); $now->setTimezone(new DateTimeZone(date_default_timezone_get())); echo 'With Yesterday Readable: '.$now->format('m/d/Y h:i:s A').' -------- '.date('m/d/Y h:i:s A').'

'; echo 'With Tomorrow: '.with($tomorrow,$timezone).'
'; $now = new DateTime('@'.with($tomorrow,$timezone)); $now->setTimezone(new DateTimeZone(date_default_timezone_get())); echo 'With Yesterday Readable: '.$now->format('m/d/Y h:i:s A').' -------- '.date('m/d/Y h:i:s A').'

'; echo 'Without Tomorrow: '.without($tomorrow,$timezone).'
'; $now = new DateTime('@'.without($tomorrow,$timezone)); $now->setTimezone(new DateTimeZone(date_default_timezone_get())); echo 'With Yesterday Readable: '.$now->format('m/d/Y h:i:s A').' -------- '.date('m/d/Y h:i:s A').'

';

OUTPUTS:

With Yesterday: 1537642801

With Yesterday Readable: 09/23/2018 12:00:01 AM -------- 09/23/2018 10:05:55 PM

Without Yesterday: 1537660801

With Yesterday Readable: 09/23/2018 05:00:01 AM -------- 09/23/2018 10:05:55 PM

With Tomorrow: 1537729199

With Yesterday Readable: 09/23/2018 11:59:59 PM -------- 09/23/2018 10:05:55 PM

Without Tomorrow: 1537747199

With Yesterday Readable: 09/24/2018 04:59:59 AM -------- 09/23/2018 10:05:55 PM

The easiest (and most reliable) way to store the time in a database table is with a timestamp. It is also the most convenient way of working out time scales as you don't have to do calculations in base 60. In MySQL this is accomplished by the UNIXTIME() function, which can be reversed by using another MySQL function called FROM_UNIXTIME().

However, you can sometimes be left with timestamps in your code and the task of trying to figure out what to do with them.

The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.

function timestamp($t = null){
 if($t == null){
  $t = time();
 }
 return date('Y-m-d H:i:s', $t);
}

And if you ever have a the opposite problem then here is a PHP function that converts a date string into a timestamp. At the moment the string needs to be in the format YYYY-MM-DD HH:MM:SS, which is what the previous function produced. This isn't too difficult to change, just alter the parameters and order of the explode(' ', $str).

function convert_datetime($str) {
 
 list($date, $time) = explode(' ', $str);
 list($year, $month, $day) = explode('-', $date);
 list($hour, $minute, $second) = explode(':', $time);
 
 $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
 
 return $timestamp;
}

Here is an example of the functions in use.

echo timestamp(convert_datetime('2008-05-10 20:56:00')). '  '. convert_datetime('2008-05-10 20:56:00') . ' 1210467360';

Add new comment

(PHP 4, PHP 5, PHP 7, PHP 8)

strtotimeParse about any English textual datetime description into a Unix timestamp

Description

strtotime(string $datetime, ?int $baseTimestamp = null): int|false

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in baseTimestamp, or the current time if baseTimestamp is not supplied. The date string parsing is defined in Date and Time Formats, and has several subtle considerations. Reviewing the full details there is strongly recommended.

Warning

The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

Parameters

datetime

A date/time string. Valid formats are explained in Date and Time Formats.

baseTimestamp

The timestamp which is used as a base for the calculation of relative dates.

Return Values

Returns a timestamp on success, false otherwise.

Errors/Exceptions

Every call to a date/time function will generate a E_WARNING if the time zone is not valid. See also date_default_timezone_set()

Changelog

VersionDescription
8.0.0 baseTimestamp is nullable now.

Examples

Example #1 A strtotime() example

echo strtotime("now"), "\n";
echo 
strtotime("10 September 2000"), "\n";
echo 
strtotime("+1 day"), "\n";
echo 
strtotime("+1 week"), "\n";
echo 
strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo 
strtotime("next Thursday"), "\n";
echo 
strtotime("last Monday"), "\n";
?>

Example #2 Checking for failure

$str 'Not Good';

if ((

$timestamp strtotime($str)) === false) {
    echo 
"The string ($str) is bogus";
} else {
    echo 
"$str == " date('l dS \o\f F Y h:i:s A'$timestamp);
}
?>

Notes

Note:

"Relative" date in this case also means that if a particular component of the date/time stamp is not provided, it will be taken verbatim from the baseTimestamp. That is, strtotime('February'), if run on the 31st of May 2022, will be interpreted as 31 February 2022, which will overflow into a timestamp on 3 March. (In a leap year, it would be 2 March.) Using strtotime('1 February') or strtotime('first day of February') would avoid that problem.

Note:

If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).

Note:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

Note:

Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub().

See Also

  • DateTimeImmutable
  • DateTimeImmutable::createFromFormat() - Parses a time string according to a specified format
  • Date and Time Formats
  • checkdate() - Validate a Gregorian date
  • strptime() - Parse a time/date generated with strftime

There are no user contributed notes for this page.

How can I timestamp in PHP?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.

What does Strtotime mean in PHP?

Definition and Usage The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How do you convert timestamps to hours?

Converting hours # There are 60 seconds in a minute, and 60 minutes in an hour. We'll divide our timestamp by 60 , and then by 60 again.

How do I convert a date to time stamp?

To convert date to timestamp, a formula can work it out. Select a blank cell, suppose Cell C2, and type this formula =(C2-DATE(1970,1,1))*86400 into it and press Enter key, if you need, you can apply a range with this formula by dragging the autofill handle.