PHP- Convert negative time-stamp to positive

时间:2015-11-12 11:45:53

标签: php laravel sphinx

I stored "date of birth" in Sphinx search engine as timestamp (integer)

(eg)

User DOB is "12-01-1960" (Age is 55)
Sphinx side: 3980298496 (MySQL handle this from DB to Sphinx)

In PHP side, For search purpose, I want to calculate time-stamp as follows but it gives negative value because PHP gives negative value if date is less than Jan 1 1970

Carbon::now()->subYears('55')->timestamp = -288316800

How do I make a positive time-stamp? so that I can do filter search from PHP. Or please suggest any other workaround.

-288316800 to 3980298496

2 个答案:

答案 0 :(得分:1)

Sphinx的timestamp属性是无符号的32位整数。 (它与uint属性没有任何不同)

...因此您无法将此值直接存储在timestamp属性中。

Sphinxes时间戳不适用于1970年之前的日期(0时间戳)

我个人使用mysql TODAYS函数为日期获取一个很好的简单整数,并将其存储在sphinx属性中。很容易使用(虽然没有模仿转换为PHP函数,所以仍然

private string Name { get; set; }

运行查询时。

(也可以在原始时间戳上添加一个大的偏移量,使其成为一个正整数,但这也否定了使用日期处理中构建的sphinxs的便利性)

答案 1 :(得分:0)

I think you can handle it by using the DateTime object

$dateTime = new DateTime;
$dateTime->setTimestamp(-288316800);

var_dump($dateTime->format('Y')); // prints string(4) "1960"

$dateTime = DateTime::createFromFormat('Y-m-d', '1930-03-03');

var_dump($dateTime->getTimestamp()); // prints int(-1256990210);

Basically you don't care how it is stored in database, you can use the DateTime object to convert from/to timestamp any integer (positive or negative)

相关问题