Asked  10 Months ago    Answers:  5   Viewed   10 times

Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?

hello,

we have this function to convert a timestamp to datetime:

$datetime = date('Y-m-d H:i:s', $timestamp);

is there a function to do the opposite?

datetime to timestamp.

Thanks

 Answers

4
$timestamp = strtotime($datetime);

Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.

Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.

EDIT:

A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:

$dateTimeObject = DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');

See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.

Monday, August 2, 2021
 
weegee
 
2

take a look at the date fortmats. if you use - as separator, php assumes it's y-mm-dd, but your order is dd-mm-y. the easiest way would be to change the separator to . wich would then be dd.mm.y.

Saturday, May 29, 2021
 
turik
 
5

Assuming you're using MS SQL Server (Which you're not, see the Update below):

A table can have only one timestamp column. The value in the timestamp column is updated every time a row containing a timestamp column is inserted or updated. This property makes a timestamp column a poor candidate for keys, especially primary keys. Any update made to the row changes the timestamp value, thereby changing the key value. If the column is in a primary key, the old key value is no longer valid, and foreign keys referencing the old value are no longer valid. If the table is referenced in a dynamic cursor, all updates change the position of the rows in the cursor. If the column is in an index key, all updates to the data row also generate updates of the index.

Information on MSDN

If you need to store date/time information against a row, and not have that date/time change, use DateTime; otherwise, use Timestamp.

Also Note: MS SQL Server timestamp fields are not Dates nor Times, they are binary representations of the relative sequence of when the data was changed.

Update

As you've updated to say MySQL:

TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the current time zone for retrieval. (This occurs only for the TIMESTAMP data type, not for other types such as DATETIME.)

Quote from MySQL Reference

More notably:

If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored.

So if you are using an application across timezones, and need the date/time to reflect individual users settings, use Timestamp. If you need consistency regardless of timezone, use Datetime

Tuesday, July 27, 2021
 
edorian
 
5

You can do almost everything with the date/time operators:

select timestamp '2014-01-10 20:00:00' +
       random() * (timestamp '2014-01-20 20:00:00' -
                   timestamp '2014-01-10 10:00:00')
Wednesday, July 28, 2021
 
2

Try this:

startLive = new Date(<?php echo strtotime($start_date)*1000; ?>);

Explanation:

PHP's strtotime function returns a Unix timestamp (seconds since 1-1-1970 at midnight).

Javascript's Date() function can be instantiated by specifying milliseconds since 1-1-1970 at midnight.

So multiply seconds by 1000 and you get milliseconds, which you can use in Javascript.

Thursday, July 29, 2021
 
EnTee
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :