How to convert this date with php - javascript

How to convert this date format that is coming from javascript:
Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time)
These formats are not working
$date = date('c', strtotime(Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time));
$date = date('Y-m-d h:i:s', strtotime(Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time));
The date goes back to 1969
Any insights?

may you should try skip "(Brasilia Standard Time)" in the data submit.
$date = date('Y-m-d h:i:s', strtotime("Thu Mar 04 2021 18:00:00 GMT-0300"));
var_dump($date);
I got this result:
string(19) "2021-03-04 01:00:00"
Edit: If you can 'skip' that string, you can do something like:
$strTime = "Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time)";
$strTime = str_replace($strTime, ""," (Brasilia Standard Time)");
$date = date('Y-m-d h:i:s', strtotime($strTime));
var_dump($date);
output:
string(19) "1969-12-31 04:00:00"

I would suggest using the modern DateTime interface and specifying a proper format, rather than having PHP try to guess how to interpret the date string. You can also use the + specifier to ignore trailing data rather than having to edit your input in advance.
$string = 'Thu Mar 04 2021 18:00:00 GMT-0300 (Brasilia Standard Time)';
$date = DateTime::createFromFormat('D M d Y H:i:s T+', $string);
var_dump($date, $date->format('c'));
Output:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2021-03-04 18:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "-03:00"
}
string(25) "2021-03-04T18:00:00-03:00"

Related

Transform date to wire format (YYYY-MM-DD) in javascript

Having this input: Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time), is there a way to format it as YYYY-MM-DD, to it will become 2021-02-03?
Try this:
const date = moment(new Date('Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)')).format('YYYY-MM-DD');
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
Parsing a timestamp to a date to reformat it may well produce incorrect results if the timestamp includes an offset or timezone. Given "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)", for a user with an offset of +1 or less the date will be 2 Feb, not 3 Feb.
The most reliable method is to reformat the string, e.g.
let timestamp = "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)";
let months = [,'Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
let pad = n=>('0'+n).slice(-2);
let p = timestamp.split(' ');
console.log(`${p[3]}-${pad(months.indexOf(p[1]))}-${p[2]}`);

How can I convert my date "Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) " to JAN 01,2020 using JavaScript?

Currently I am getting my date from data base in the format of Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) but I want the date in JAN O1,2020 format and I don't want to use mon=ment.js for this. any other ways to achieve this?
Here is a solution without moment.js
var n = new Date("Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)");
var options = {
month: "short",
day: "numeric",
year: "numeric",
timeZone: 'IST'
}
document.write(n.toLocaleString("en-EN", options));
You can simply use moment.js
import * as moment from 'moment';
moment('your_date_variable').format('DD/MM/YYYY');

how can I Convert "Tue Nov 26 2019 16:00:00 GMT-0800" to .net json date formate "\/Date(1574812800000)\/" formate?

I need to conver my ison date formate to .net json date formate in javascript
like this
Tue Nov 26 2019 16:00:00 GMT-0800" to "/Date(1574812800000)/"
With JavaScript:
var dt = new Date("Tue Nov 26 2019 16:00:00 GMT-0800");
var format = "/Date("+dt.valueOf()+")/";
console.log(format);
You can use Moment.js:
moment("Tue Nov 26 2019 16:00:00 GMT-0800", "ddd MMM D YYYY H:m:s [GMT]ZZ").format("[/Date(]x[)/]")
You should get the result:
/Date(1574812800000)/
The timestamp you supplied corresponds to May 23, 2001 at midnight GMT.

Convert js date to php timestamp

Hi i am using js datetime and i sent this variable to php. Now i want to convert this js date to php datetime, but its not working. Here is what i want.
input :
Mon Jul 31 2017 06:00:00 GMT+0500 (Pakistan Standard Time)
Outut:
2017-07-31 06:00:00
Here is my code:
$dispatchDate = "Mon Jul 31 2017 06:00:00 GMT+0500 (Pakistan Standard Time)";
$dispatchDate = substr($dispatchDate, 0, strpos($dispatchDate, '('));
echo date('Y-m-d h:i:s', strtotime($dispatchDate));
But its not working.
You are getting time in GMT convert your time in PST
<?php
$dispatchDate = "Mon Jul 31 2017 06:00:00 GMT+0500 (Pakistan Standard Time)";
$dispatchDate = substr($dispatchDate, 0, strpos($dispatchDate, '('));
echo date('Y-m-d h:i:s', strtotime($dispatchDate." +5 hours"));
?>
live demo : https://eval.in/836779
working solution and I think easiest : convert your javascript datetime into a int (timestamp) send the timestamp to your PHP and convert back your timestamp to a PHP datetime.
date('Y-m-d H:i:s', your timestamp)

Javascript vs Date

I have some interesting problem and can't find the solution. Look at this:
var d1 = new Date("07 31 2014");
document.write(d1);
document.write('<br />');
var d2 = new Date(1406746800 * 1000);
document.write(d2);
when I run this script I get this result:
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
then after I changed my time zone I get this result:
Thu Jul 31 2014 00:00:00 GMT-0800 (AKDT)
Wed Jul 30 2014 11:00:00 GMT-0800 (AKDT)
as you can see the second result is Jul 30, but first result is Jul 31. I think they must both be equal to 31 Jul. I know this problem depends on the timezone but is there a solution?
So the constructor parameter is:
an Integer value representing the number of milliseconds since 1
January 1970 00:00:00 UTC
So given your integer value, that represents (for me, in BST):
Wed Jul 30 2014 20:00:00 GMT+0100
Which is
Wed Jul 30 2014 19:00:00 UTC
And your timezone is GMT-8, so thats the above -8 which gives:
Wed Jul 30 2014 11:00:00 GMT-0800 AKDT
The date string constructor constructs the date in your local timezone. You can see what the value should be by doing this:
new Date("07 31 2014").getTime() / 1000
Which returns 1406761200, not 1406746800

Categories

Resources