Phrancis
Phrancis

Reputation: 2282

date strtotime() calculation returns 1969 date

I have the following code getting $_POST from HTML form:

<?php
$departureTime = date("l, Y-M-d H:i:s");
// calculate time-traveled time
$arrivalTime = date( "l, Y-M-d H:i:s", strtotime(
        "$departureTime
        .' '
        .$travelTimeOperator        // '+' or '-'
        .$timeAmount            // integer or decimal (tested with integer)
        .' '
        .$travelTimeInterval") );   // 'days' or 'hours'

echo "$timeAmount, $departureTime, $arrivalTime";
?>

I get the following output, using 3 as my $timeAmount, - as my operator and days as $travelTimeInterval:

3, Friday, 2014-Oct-17 04:36:43, Wednesday, 1969-Dec-31 19:00:00

How can I fix the 1969 date? I've read something about Unix timestamps but I can't get it to work. In this case it is intended to subtract 3 days. I get the exact same value 1969-Dec-31 19:00:00 if I try to add days instead.

PS: I used line breaks in my concatenate to make it easier to troubleshoot, I realize it is not standard.

Upvotes: 2

Views: 1387

Answers (4)

jan
jan

Reputation: 2958

your value in strtotime() cannot be interpreted as a valid date string by php.

When this happens, php uses the timestamp 0 which is 1970-1-1 00:00:00. You subtract 5 hours (your timezone) from that date which gives you 1969-12-31 19:00:00.

try this:

$arrivalTime = date("l, Y-M-d H:i:s",
strtotime($travelTimeOperator . $timeAmount . ' ' .$travelTimeInterval));

Upvotes: 3

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2125

The string supplied to strtotime is invalid. You started a double quote, but you didn't end it correctly:

    $departureTime = date("l, Y-M-d H:i:s");
$travelTimeOperator = '+';
$timeAmount = 1;
$travelTimeInterval = 'days';
$departureTimeString = "$departureTime
        .' '
        .$travelTimeOperator        // '+' or '-'
        .$timeAmount            // integer or decimal (tested with integer)
        .' '
        .$travelTimeInterval";
echo $departureTimeString;
// Friday, 2014-Oct-17 11:20:43 .' ' .+ // '+' or '-' .1 // integer or decimal (tested with integer) .' ' .days

You can do something like this to solve it:

<?php
$departureTime = date("l, Y-M-d H:i:s");

$travelTimeOperator = '+';
$timeAmount = 1;
$travelTimeInterval = 'days';
$departureTimeString = "$departureTime "
                       . $travelTimeOperator
                       . $timeAmount
                       . " $travelTimeInterval";
$arrivalTime = date( "l, Y-M-d H:i:s", strtotime($departureTimeString));

echo "$timeAmount, $departureTime, $arrivalTime";
// 1, Friday, 2014-Oct-17 11:22:23, Saturday, 2014-Oct-18 11:22:23

Using object oriented features, you can do something like this:

<?php
$departureTime = new DateTimeImmutable();

$travelTimeOperator = '+';
$timeAmount = 1;
$travelTimeInterval = 'days';
$relativeDateString = "{$travelTimeOperator}{$timeAmount} $travelTimeInterval";
$arriveTime = $departureTime->modify($relativeDateString);

echo $arriveTime->format('l, Y-M-d H:i:s');
// Saturday, 2014-Oct-18 11:26:15

Upvotes: 5

Gipsz Jakab
Gipsz Jakab

Reputation: 431

<?php
 $departureTime = date("l, Y-M-d H:i:s");
 if ($travelTimeInterval=='days') {$unit=(24*3600);} else {$unit=3600;}
 $diff=$travelTimeOperator.($unit*$timeAmount);
 $arrtime=time()+$diff;
 $arrivalTime = date( "l, Y-M-d H:i:s", $arrtime);
 echo "$timeAmount, $departureTime, $arrivalTime";
?>

This should do the trick if you dont want to change your programming method to object oriented.

Upvotes: 2

Elias
Elias

Reputation: 1560

I think you are abusing strtotime for performing calculations on dates and times supplied as strings? Try the DateTime class. It has a modify method.

For example:

$date = new DateTime(); // now
$date->modify('+2 hours'); // add two hours
echo $date->format('l, Y-M-d H:i:s');

Upvotes: 2

Related Questions