Talha Malik
Talha Malik

Reputation: 1559

php strtotime giving wrong results

When I use the strtotime function it gives me different results on two different servers. One of them gives me the correct answer. It has php version 5.2.10.

On other server it gives me wrong date which has php version 5.0.4. I think that difference is the reason.

Code:

$fromDate = $_POST['fromDate']; // 25-11-2013
$strFromDate = date("Y-m-d", strtotime($fromDate));
echo $strFromDate; // 2018-05-06

Could anyone help me with this?

Upvotes: 1

Views: 600

Answers (2)

Glavić
Glavić

Reputation: 43582

This probably happens because PHP 5.0.4 doesn't yet support d-m-Y format, see here.

Try to change format d-m-Y to Y-m-d. Here is one simple example:

$fromDate = '25-11-2013';
sscanf($fromDate, '%d-%d-%d', $d, $m, $Y);
$strFromDate = date("Y-m-d", strtotime("$Y-$m-$d"));
echo $strFromDate;

This will work on all PHP 4.x and 5.x versions

Upvotes: 1

Andrew
Andrew

Reputation: 1776

Maybe the problem is related to locales. Check if it is matching on both server.

Upvotes: 0

Related Questions