开发者

How do i know if a time is elapsed/expired in php?

开发者 https://www.devze.com 2023-02-14 15:48 出处:网络
Here\'s the scenario I would like to know if a date like \'2011-03-04\' and time like \'17:06:00\' in a specific TimeZones like \'IST/PDT/CDT\' is elapsed by using php?

Here's the scenario

I would like to know if a date like '2011-03-04' and time like '17:06:00' in a specific TimeZones like 'IST/PDT/CDT' is elapsed by using php?

Is there any function available to check this

 $strDate = '2011-03-04 17:06:00';
 $TZ      = 'IST';

 $strDate = '2011-02-01 11:16:30';
 $TZ      = 'CDT';

 $strDate = '2010-08-04 07:20:00';
 $TZ      = 'IST';

 $strDate = '2011-02-27 09:55:00';
 $TZ      = 'PST';

I've tried/searched all over the net but no joy, please help

Yeah i tried it using DateTime but it doesnt work for me.. My server is in IST TimeZone

$date = new DateTime('2011-03-04 17:06:00');
$date->setTimeZone(new DateTimeZone("PST"));
$newtime = $date->getTimestamp();
$time     = time();
if ($time > $usertime) 
    echo 'time passed';
el开发者_运维知识库se
    echo 'time NOT passed';

Thanks to tobyS but it too falied

$elapsedTime = new DateTime(
    '2011-03-04 17:00:00', // I set it to 5PM. Now the time is 5:45 PM here in INDIA
    new DateTimeZone( 'IST' )
);

$elapsedInt = $elapsedTime->diff( new DateTime() );
echo ( $elapsedInt->invert ? 'Future' : 'Past' ) . "\n";


You can create a DateTime object from your time data and use its diff() method against the current time.

Example code:

<?php

$elapsedTime = new DateTime(
    '2010-08-04 07:20:00',
    new DateTimeZone( 'IST' )
);

$notElapsedTime = new DateTime(
    '2012-08-04 07:20:00',
    new DateTimeZone( 'IST' )
);

$elapsedInt = $elapsedTime->diff( new DateTime() );

echo ( $elapsedInt->invert ? 'Future' : 'Past' ) . "\n";

$notElapsedInt = $notElapsedTime->diff( new DateTime() );

echo ( $notElapsedInt->invert ? 'Future' : 'Past' ) . "\n";

?>


PHP doesn't understand those timezones:

$timezone = 'America/New_York';
$date = new DateTime($strDate, new DateTimeZone($timezone));
0

精彩评论

暂无评论...
验证码 换一张
取 消