0

At browser,in JS

var today = new Date()
todaySendToServer = today.toString();

I am sending todaySendToServer to server in AJAX call or as part of URL.

At server, in PHP:

$todayJsDateString1 = preg_replace('#^(\d+)/(\d+)/(\d+)\s([\d:]+)\s([a-zA-z]{0,2})$#','$3-    $2-$1 $4 $5', $todayJsDateString); 
$todayTimestamp = strtotime(todayJsDateString1);

The strtotime() PHP call returns false for date strings returned by some browser(like IE9)

Is there any alternate way to achieve this?

2
  • Can you format your string according to a standard? for example ISO8601 Commented Sep 3, 2012 at 7:00
  • How to format JavaScript Date string to ISO8601? Is there any API? Commented Sep 3, 2012 at 12:40

3 Answers 3

3

Pass the timestamp instead, so you even don't need to use strtotime in the php side.

var today = new Date();
var ts = today.getTime() / 1000;
Sign up to request clarification or add additional context in comments.

Comments

1

It's just a simple typo I guess, you missed a dollar sign:

$todayJsDateString1 = preg_replace('#^(\d+)/(\d+)/(\d+)\s([\d:]+)\s([a-zA-z]{0,2})$#','$3-    $2-$1 $4 $5', $todayJsDateString); 
$todayTimestamp = strtotime($todayJsDateString1);

1 Comment

It didn't work for JS date string sent from IE9 browser. Thank you
1

Why not send it as a timestamp all the way?

var timestamp = new Date().getTime() / 1000;

$todayTimestamp = $todayJsDateString;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.