php中timetotime,php time to systemtime

慕容兴贤
2023-12-01

收到远端php送来的一个时间值, 为一串数字. 查了下,好像是使用 php的 time() 或 date() 生成的.

整理了一个函数,  将该时间戳转成 SYSTEMTIME.

因为我知道该时间值的大概实际日期,转换后感觉还靠谱, 像是正确的时间值.

函数:

/**

struct tm declare

tm_sec

Seconds after minute (0 – 59).

tm_min

Minutes after hour (0 – 59).

tm_hour

Hours since midnight (0 – 23).

tm_mday

Day of month (1 – 31).

tm_mon

Month (0 – 11; January = 0).

tm_year

Year (current year minus 1900).

tm_wday

Day of week (0 – 6; Sunday = 0).

tm_yday

Day of year (0 – 365; January 1 = 0).

tm_isdst

Always 0 for gmtime.

*/

BOOL php_time_2_systemtime(long lPhpTime, SYSTEMTIME* pst)

{

BOOL bRc = FALSE;

time_t tmt = -1;

struct tm* pGmt = NULL;

do

{

if (NULL == pst)

break;

tmt = lPhpTime;

pGmt = gmtime(&tmt);

if (NULL == pGmt)

break;

pst->wYear = 1900 + pGmt->tm_year;

pst->wMonth = 1 + pGmt->tm_mon;

pst->wDay = pGmt->tm_mday;

pst->wHour = pGmt->tm_hour;

pst->wMinute = pGmt->tm_min;

pst->wSecond = pGmt->tm_sec;

pst->wDayOfWeek = pGmt->tm_wday;

bRc = TRUE;

} while (0);

return bRc;

}

函数的使用:

if (NULL != m_p_ctrl_time)

{

if (ns_base::php_time_2_systemtime(_ttol(str_time.c_str()), &st))

{

strTemp = ns_base::StringFormatV(L"%d-%d-%d",

st.wYear,

st.wMonth,

st.wDay);

}

else

{

strTemp = L"";

}

m_p_ctrl_time->SetText(strTemp.c_str());

}

 类似资料: