0

i need to create a unix timestamp in milliseconds like 1319031463499 to post to an web service.

tried all sorts and cant get it.

i have googled and keep getting strange years?

4
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by mschroeder
0
echo microtime(true);

Like that? Without the 'true' you get a string.

ok microtime(true) gave me 1319055163.0786 OR 19-10-2011 @ 11:12:43

and microtime() gave me 0.07860300 1319055163 - 01-01-1970 @ 03:00:00

i need the 13 digit??

Edited by vlowe: n/a

0

I just get 1319055897.738 with the true parameter. This is 1319055897 seconds with 738 milliseconds).

0

ok i think i have it with

$millitime = round(microtime(true) * 1000);

any idea how i can get time minus 24hrs, minus 1 week and minus 30 days?

0

vlowe,

[edit]
Err, I misread that initially
[/edit]

The PHP DateTime library would be my preferred method of doing what you want.

<?php

//Only need to specify parameters if your timezone is not set.
$now = new DateTime( 'now', new DateTimeZone( 'America/New_York' ) ); //1319061332  or 10/19/2011 @ 4:55pm

//If your timezone is set in php.ini
//$now = new DateTime(); //1319061332  or 10/19/2011 @ 4:55pm

$timestamp = $now->getTimestamp(); //1319061332

//Minus 24 hours
$past24hrs = $now->sub( new DateInterval( 'P24H' ) );
$timestamp = $past24hrs->getTimestamp();

//Minus 1 week
$past1week = $now->sub( new DateInterval( 'P1W' ) );
$timestamp = $past1week->getTimestamp();

//Minus 30 days
$past30days = $now->sub( new DateInterval( 'P30D' ) );
$timestamp = $past30days->getTimestamp();

http://www.php.net/manual/en/class.datetime.php
http://www.php.net/manual/en/class.datetimezone.php
http://www.php.net/manual/en/class.dateinterval.php

[edit]
If you need a timestamp instead in milliseconds simply multiply the returned timestamps by 1000
[/edit]

Edited by mschroeder: n/a

0

i use the following quite frequently to modify my dates.

$newTimeStamp=strtotime(date('m/d/Y H:m:sA', $oldTimeStamp) . " + 4 hours");

I find this to be fairly quick and easy when programming cause it is a single line.

1

@ajbest

Just be aware that there are limitations to strtotime and similar functions. The y2k38 bug is when the unix timestamp will exceed a 32bit integer. So for example, if you would try to generate a date, say 30 years in the future (maybe the end date of a mortgage), it will fail.

The php DateTime classes do no suffer from this same limitation. A 64bit build of php will also solve this.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.