Convert js date to php timestamp - javascript

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)

Related

How to convert this date with php

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"

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.

Date Formatting and filtering in angularjs

I want to convert "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)" date format to "YYYY/MM/DD" in my controller.
Try to do this in this way:
var date = "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)";
var newDate = $filter('date')(new Date(date), 'yyyy/MM/dd');
Angular date

convert date from 23-08-2015 00:00:00 to Tue Aug 23 2015 00:00:00 GMT+0530

I have date in 23-08-2015 00:00:00 format in my controller and pass it to view using ViewData. I want to convert this date to Tue Aug 23 2015 00:00:00 GMT+0530 format.. Is it possible with controller or can I convert it in my view using jquery?
Can anyone help me to solve this?
In javascript, if you simply create a new Date object with the string that you have, you'll get the desired format.
var testdate = new Date("23-08-2015 00:00:00");
console.log(testdate);
Output:
Tue Nov 08 2016 00:00:00 GMT+0530 (India Standard Time)
In your controller:
DateTime date = DateTime.Now;
date.ToLongDateString();
I done it by using following format:
string startDateCalendar = Convert.ToString(startDate.ToString("ddd MMM dd yyyy HH:mm:ss")) + " GMT+0530";
using JavaScript it is like so:
function parseDDMMYYYY(d){
return new Date(
d.replace(/^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/,"$3-$2-$1 $4:$5:$6")
)
}
parseDDMMYYYY('23-08-2015 00:00:00')

Converting a date string into UTC+0530 format using javascript

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?
Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.
I tried this code and it returned proper date (In Indian Locale)
var d=Date.parse("14,Feb,2011");
document.write(new Date(d));
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
Here's an example of converting between different time zones.
<html>
<body>
<script type="text/javascript">
//Set you offset here like +5.5 for IST
var offsetIST = 5.5;
//Set you offset here like -8 for PST
var offsetPST = -8;
//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));
//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate = new Date(d.getTime() + (d.getTimezoneOffset()*60000));
//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST*60)*60000));
//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate= new Date(utcdate.getTime() - ((-offsetPST*60)*60000));
document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>
</body>
</html>
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time)
Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.
var d = new Date("14-Feb-2011");
this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

Categories

Resources