PHP: strtotime not work from file_get_contents - javascript

Hello i try to convert date to strtotime but my code not work;
time.html - show current time like 2017-03-09 07:11:01
time.html
<script type="text/javascript">
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0' + month;
}
if(day.toString().length == 1) {
var day = '0' + day;
}
if(hour.toString().length == 1) {
var hour = '0' + hour;
}
if(minute.toString().length == 1) {
var minute = '0' + minute;
}
if(second.toString().length == 1) {
var second = '0' + second;
}
var dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
return dateTime;
}
document.write(getDateTime());
</script>
index.php
$file = file_get_contents("http://localhost/time.html");
$file = strtotime($file);
echo "Time: ". $file;
code work like this: strtotime("2017-03-09 07:11:01"); but i need with file_get_contents to get real time (current time), thanks in advance

javascript is client-side, so you cant get it from php, time.html will only print the date when a user reads it (will show the user time). If you want print the time in php you should use the php function date(), so you will get the server date (php is server-side), for example:
echo "Time: ". date("Y/m/d H:i:s");
Note: strtotime() is a function to check or convert a string to time format

Related

JQuery date and time in mysql databse datetime format

I want to store my country date and time in JavaScript var variable using JQuery. That date and time should be in default mysql database datetime format. JQuery date and time should not be local date time and not for auto get timezone date time. That should be UTC +09:30 date and time in set.
PHP Code for that in mysql datetime format:
CONVERT_TZ(NOW(), 'UTC', '+09:30')
OR
Tell me how to convert JQuery NOW() date time data to mysql datetime format in PHP.
Final answer should be date and time in mysql datetime format and that should be in javascript var veritable or PHP veritable.
Javascript date and time will be in local timezone.
If you want to display the time just like mysql time, then you will have to convert the localtime to server timezone in javascript.
You can you date.getTimezoneOffset(), to get the offset between local time and utc, and then use this to convert locatime to utc+9:30
Edit
how to convert JQuery NOW() date time data to mysql datetime format in
PHP.
Final answer should be date and time in mysql datetime format and that
should be in javascript var veritable or PHP veritable.
Example to convert locatime to different timezone ( UTC + 9:30 )
var localDate = $.now();
localDate = new Date(localDate);
console.log("local datetime is ", printMysqlFormat(localDate));
var offset = localDate.getTimezoneOffset();
var offsetMilliseconds = offset * 60 * 1000;
var serverMilliseconds = localDate.getTime() + offsetMilliseconds + (570 * 60 * 1000)
var serverdate = new Date(serverMilliseconds);
console.log("localtime in server timezone is", printMysqlFormat(serverdate));
function printMysqlFormat(d) {
var day = d.getDate() + "";
var month = (d.getMonth() + 1) + "";
var year = d.getFullYear() + "";
var hour = d.getHours() + "";
var minutes = d.getMinutes() + "";
var seconds = d.getSeconds() + "";
day = day.length == 1 ? "0" + day : day;
month = month.length == 1 ? "0" + month : month;
hour = hour.length == 1 ? "0" + hour : hour;
minutes = minutes.length == 1 ? "0" + minutes : minutes;
seconds = seconds.length == 1 ? "0" + seconds : seconds;
return day + "-" + month + "-" + year + " " + hour + ":" + minutes + ":" + seconds;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

convert javascript date to c# datetime

I am trying to convert javascript date to c# datetime
JavaScript Code
var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var year = date.getFullYear();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
// After this construct a string with the above results as below
var JSDateString = year+ "-" + month + "-" + day + " " + hour + ':' + minute + ':' + second;
C# Code
var JSDateString = "2016-04-02 17:15:45"; // I receive date string via Ajax call in this format
var dt = DateTime.ParseExact(JSDateString , "yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture);
I get invalid datetime format exception. I researched other options in internet but I didn't find any specific answer on how to convert JavaScript datetime to C# datetime.
mm is for minutes, you want MM for month:
var dt = DateTime.ParseExact(JSDateString , "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
This might help with the JavaScript side:
function getDate() {
var date = new Date(),
year = date.getFullYear(),
month = (date.getMonth() + 1).toString(),
formatedMonth = (month.length === 1) ? ("0" + month) : month,
day = date.getDate().toString(),
formatedDay = (day.length === 1) ? ("0" + day) : day,
hour = date.getHours().toString(),
formatedHour = (hour.length === 1) ? ("0" + hour) : hour,
minute = date.getMinutes().toString(),
formatedMinute = (minute.length === 1) ? ("0" + minute) : minute,
second = date.getSeconds().toString(),
formatedSecond = (second.length === 1) ? ("0" + second) : second;
return year + "-" + formatedMonth + "-" + formatedDay + " " + formatedHour + ':' + formatedMinute + ':' + formatedSecond;
};
View a fiddle here: https://jsfiddle.net/kpduncan/de8j318k/
I had too do something like this when I building an application due to not being allowed to add thrid party JS and needing support back to IE8.
As you can see on the MSDN, mm is for minutes (00 - 59) whereas MM is for the month (01 - 12).
var JSDateString = "2016-04-02 17:15:45";
var formatCode = "yyyy-MM-dd HH:mm:ss";
var dt = DateTime.ParseExact(JSDateString , formatCode, CultureInfo.InvariantCulture);
You can see that mm is for minutes because you already use it in your HH:mm:ss.

How to alert current date in yyyy-mm-dd hrs:min:sec format in javascript?

In my program i need to alert current date with time in specific format(2015-11-18 12:23:00) so i am write like this
var date = new Date();
alert(date);
but the result is Wed Nov 18 2015 12:24:28 GMT+0530 (India Standard Time).
and also i am try like this
<script>
var d = new Date();
var c = new Date();
alert(formatDate(c));
alert(formatDate(d));
function formatDate(d)
{
var month = d.getMonth();
var day = d.getDate();
var hours = d.getHours();
var minutes = d.getMinutes();
month = month + 1;
month = month + "";
if (month.length == 1)
{
month = "0" + month;
}
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
hour = hour + "";
if (hour.length == 1)
{
hour = "0" + hour;
}
minute = minute + "";
if (minute.length == 1)
{
minute = "0" + minute;
}
return d.getFullYear()+month + '' + day + ''+ hour + '' + minute + '';
}</script>
it is also not working properly.
how can i do this in javascript and also i need to passed the veriable to database in another php file. please help me how can i do this
I think this below code are helpful us.
function getDateTimeFormate () {
now = new Date();
year = "" + now.getFullYear();
month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; }
hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; }
minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
To get date time formate like.
alert(getDateTimeFormate());
// example alert message: 2011-05-18 15:20:12
Try this..
<script>
var currentdate = new Date();
var datetime = currentdate.getFullYear() + "-"
+ (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
alert(datetime);
</script>
Output:2015-11-18 12:46:52
Demo:http://js.do/code/73749
Please try the following code:
Number.prototype.padLeft = function(base,chr){
var len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}
var d = new Date;
function formatDate(d){
dformat = [ (d.getMonth()+1).padLeft(),
d.getDate().padLeft(),
d.getFullYear()].join('-')+
' ' +
[ d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':');
return dformat;
}
alert(formatDate(d));
It will return : 11-18-2015 12:17:02. And to pass the value to a php code check this: How to pass JavaScript variables to PHP?
This might be useful
Date.prototype.yyyy = function() {
var yy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString();
var dd = this.getDate().toString();
var hh = this.getHours().toString();
var min = this.getMinutes().toString();
var ss = this.getSeconds().toString();
return yy +"-" +(mm[1]?mm:"0"+mm[0]) + "-" +(dd[1]?dd:"0"+dd[0])+" "+(hh[1]?hh:"0"+hh[0])+":"+(min[1]?min:"0"+min[0])+":"+(ss[1]?ss:"0"+ss[0]);
};
d = new Date();
d.yyyy();
From the below function you will get all the details you want to show use it according to your need
getMonth() - Returns a zero-based integer (0-11) representing the month of the year.
getDate() - Returns the day of the month (1-31).
getDay() - Returns the day of the week (0-6). 0 is Sunday, 6 is Saturday.
getHours() - Returns the hour of the day (0-23).
getMinutes() - Returns the minute (0-59).
getSeconds() - Returns the second (0-59).
getMilliseconds() - Returns the milliseconds (0-999).
getTimezoneOffset() - Returns the number of minutes between the machine local time and UTC.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var today = dd+'/'+mm+'/'+yyyy;
document.getElementById("DATE").value = today;
Use this code:
var sec = d.getSeconds();
return d.getFullYear()+'-'+month + '-' + day + ' '+ hour + ':' + minute + ':'+sec;
instead of:
return d.getFullYear()+month + '' + day + ''+ hour + '' + minute + '';

Clean up JavaScript timestamp

I'm using this javascript code to sync the client time with my server time
var offset = 0;
function calcOffset() {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("GET", "http://stackoverflow.com/", false);
xmlhttp.send();
var dateStr = xmlhttp.getResponseHeader('Date');
var serverTimeMillisGMT = Date.parse(new Date(Date.parse(dateStr)).toUTCString());
var localMillisUTC = Date.parse(new Date().toUTCString());
offset = serverTimeMillisGMT - localMillisUTC;
}
function getServerTime() {
var date = new Date();
date.setTime(date.getTime() + offset);
return date;
}
the date I get back is
"2013-10-03T16:37:05.568Z"
How to I make this "2013-10-03 H:i:s"?
Although using moment.js is a smoother way to do it if you are working with a bunch of dates, here's a way to do it with vanilla JS:
x = new Date
x.getFullYear() + '-' + x.getMonth() + '-' + x.getDay()
Edit:
Here it is with the time and leading zeros on the month and day, as you can see these extra things add a good bit more code. Maybe if you post another question detailing your troubles with moment.js, we would be able to help getting it fixed:
formatDate(new Date);
function formatDate(d){
var year = d.getFullYear();
var month = addLeadingZero(d.getMonth());
var day = addLeadingZero(d.getDay());
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
function addLeadingZero(n){ return n < 10 ? '0'+n : ''+n }

How to format a dateTime

Okay I have the following problem. I want to get the current dateTime and then want do check if a date that I enter is bigger than the current DateTime. The format of my dateTime should look like this.
03/11/2012 09:37 AM
Here is the function how I get the current DateTime.
function getCurrentDateTime()
{
var currentTime = new Date()
// Date
var month = currentTime.getMonth() + 1;
if (month < 10){
month = "0" + month;
}
var day = currentTime.getDate();
var year = currentTime.getFullYear();
// Time
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){
minutes = "0" + minutes;
}
if(hours > 11){
var dateString = month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + "PM";
test = new Date(dateString);
return dateString ;
} else {
var dateString = month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + "AM";
return dateString;
}
}
As you can see how it gives back a string. But when I want to covert it to a date with this function. I get this format Fri May 11 2012 09:37:00 GMT+0200 (Romance Daylight Time)
date = new Date(dateString);
And with this I can't calculate.
Could anybody help me how I can get the current date in this format so that I can do the check?
Kind regards.
Javascript provides very limited functionality for working with dates out of the box. Use an external library like momentjs.
For example, your function would be reduced to
var stringDate = moment().format("DD/MM/YYYY HH:mm A");
And you could convert that and compare it to the current time with
var earlierDate = moment(stringDate, "DD/MM/YYYY HH:mm A");
if (earlierDate.valueOf() < moment().valueOf()) {
// earlier indeed
}
datejs is another lib for solving date-manipulation problems

Categories

Resources