PHP-将负时间戳转换为正时间戳 [英] PHP- Convert negative time-stamp to positive

查看:272
本文介绍了PHP-将负时间戳转换为正时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将出生日期"存储在Sphinx搜索引擎中作为时间戳(整数)

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

(例如)

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

在PHP方面,出于搜索目的,我想按以下方式计算时间戳,但它给出了负值,因为如果日期小于1970年1月1日,则PHP给出了负值

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

我该如何设定一个正确的时间戳记?这样我就可以从PHP进行过滤搜索.或者,请提出其他解决方法.

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

推荐答案

Sphinx的timestamp属性是一个无符号的32位整数. (实际上与uint属性没有什么不同)

Sphinx's timestamp attribute is an unsigned 32bit integer. (its not actully any different to a uint attribute)

...因此您不能将这样的值直接存储在timestamp属性中.

... so you couldn't store such a value directly in a timestamp attribute.

狮身人面像时间戳记不适用于1970年之前的日期(0时间戳记)

Sphinxes timestamp is not good for dates prior to 1970 (0 timestamp)

我个人使用mysql TODAYS函数来获取日期的简单整数,并将其存储在sphinx属性中.相当容易使用(尽管还没有将转换模拟为php函数,所以仍然可以

Personally I use mysql TODAYS function to get a nice simple integer for a date, and store that in the sphinx attribute. Quite easy to work with (although havent emulated the conversion as a php function, so still do

<?php
$days = getOne("SELECT TODAYS('1960-01-12')");

运行查询时.

(还可以在原始时间戳上添加较大的偏移量,使其成为正整数,但这也否定了使用内置在日期处理中的狮身人面像的便利)

(Could also add a large offset to the raw timestamp to make it an positive integer, but that also negates the convenience of using sphinxes built in date processing)

这篇关于PHP-将负时间戳转换为正时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆