FranMercaes
FranMercaes

Reputation: 151

Timestamp wrong time

This is my problem

My default timezone is date_default_timezone_set('Europe/Madrid')

actual time 2012-12-06 17:00:38
start time 2012-12-10 16:30:00

unix timestamp actual time 1354809638
unix timestamp start time 1350052200

Start time is greater then actual time. But "unix timestamp actual time" is greater then "unix timestamp start time".

I wonder why?

RELEVANT CODE:

$to_unix_startTime = new DateTime($postField['startTime']);
$UnixStartTime = $to_unix_startTime->format('U');

$date = new DateTime(); $actualTime = $date->getTimestamp();

$start = new DateTime($postFields['startAuct']);
        $start->format('Y-m-d H:i:s').PHP_EOL;
        $start_tmstmp = date_timestamp_get($start);
        $date = new DateTime(); 
        $timestamp = $date->getTimestamp();
        if ($timestamp > $start_tmstmp) {
            echo $auctStatus = 1;
        } elseif ($timestamp < $start) {
            echo $auctStatus = 4;
        }

Upvotes: 1

Views: 1004

Answers (2)

Decent Dabbler
Decent Dabbler

Reputation: 22783

$postField['startTime'] is in the wrong format. How do I know this? The timestamp you got from the start date, 1350052200, represents date 2012-10-12 (October 12th 2012) in stead of 2012-12-10 (December 10th 2012).

$postField['startTime'] is probably in the format 10/12/2012 (which will be parsed by DateTime as an American style date), when it should be formatted as 10-12-2012 (which will be parsed by DateTime as a European style date).

So, concluding, check the format of $postField['startTime'] and make sure it is a European style representation of a date.

[edit] Compare:

$european = new DateTime( '10-12-2012' );
$american = new DateTime( '10/12/2012' );

echo $european->format( 'Y-m-d' ) . ' (' . $european->getTimestamp() . ')' . PHP_EOL;
echo $american->format( 'Y-m-d' ) . ' (' . $american->getTimestamp() . ')' . PHP_EOL;

Upvotes: 2

shadyyx
shadyyx

Reputation: 16055

There is no need to format the datetime before getting the timestamp.

Check my code here: https://compilr.com/shadyyx/datetime/index.php

Should there be any problem with the link, here is the code without output:

<?php

date_default_timezone_set('Europe/Madrid');

$start = new DateTime('2012-12-10 16:30:00');
$actual = new DateTime('2012-12-06 17:00:38');

echo $start->format('d.m.Y H:i:s').PHP_EOL;
echo $actual->format('d.m.Y H:i:s').PHP_EOL;
echo PHP_EOL;

$start_tmstmp = $start->getTimestamp();
$act_tmstmp = $actual->getTimestamp();

echo $start_tmstmp.PHP_EOL;
echo $act_tmstmp.PHP_EOL;
echo PHP_EOL;

var_dump($start_tmstmp > $act_tmstmp);

Upvotes: 0

Related Questions