I have a php file which I will later change into the form of an HTML file, which be current constraints are:
- Date format using Javascript.
Her script like the following snippet:
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<?php
/* Set start and end dates here */
$startDate = strtotime("15 August 2012 12:00:00");
$endDate = strtotime("15 November 2012 12:00:00");
/* /Set start and end dates here */
?>
<script type="text/javascript">
$(document).ready(function(){
JBCountDown({
secondsColor : "#ffdc50",
minutesColor : "#9cdb7d",
hoursColor : "#378cff",
daysColor : "#ff6565",
startDate : "<?php echo $startDate; ?>",
endDate : "<?php echo $endDate; ?>",
now : "<?php echo strtotime('now'); ?>",
seconds : "<?php echo date("s"); ?>"
});
});
</script>
please help me to overcome it.
I want to run a php script in javascript that is being subordinated.
example:
<script type="text/javascript">
$(document).ready(function(){
JBCountDown({
secondsColor : "#ffdc50",
minutesColor : "#9cdb7d",
hoursColor : "#378cff",
daysColor : "#ff6565",
startDate : startDate, //format time in JS
endDate : endDate, //format time in JS
now : strtotime('now'), //format time in JS
seconds : date("s")
});
});
</script>
Please help,
Regards
this can all be done in javascript.
<script type="text/javascript">
$(document).ready(function(){
//http://www.convert-unix-time.com/
var unix = Math.round(+new Date()/1000); //unix timestamp for todays date
//alert(unix);
JBCountDown({
secondsColor : "#ffdc50",
secondsGlow : "none",
minutesColor : "#9cdb7d",
minutesGlow : "none",
hoursColor : "#378cff",
hoursGlow : "none",
daysColor : "#ff6565",
daysGlow : "none",
startDate : "1362096000 ", //unix timestamp for ' Friday 1st March 2013 12:00:00 AM'
endDate : "1373328000", //unix timestamp for 'Tuesday 9th July 2013 01:00:00 AM'
now : unix,
seconds : unix % 60 //unix timestamp for seconds in realtime
});
});
</script>
Use strtotime() and date():
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
<?php
/* Set start and end dates here */
$startDate = new DateTime("15 August 2012 12:00:00");
$endDate = new DateTime("15 November 2012 12:00:00");
/* /Set start and end dates here */
print $startDate->format('FORMAT');
?>
See all date time formats: http://www.php.net/manual/en/datetime.formats.php
Related
I'm using the datetimepicker jquery plugin and have the following code to display it:
var dateToday = new Date();
$('#post_date_picker').datetimepicker({
minDate: dateToday,
timeFormat: 'hh:mm p z',
timezoneList: [
{ value: -300, label: 'Eastern'},
{ value: -360, label: 'Central' },
{ value: -420, label: 'Mountain' },
{ value: -480, label: 'Pacific' }
]
});
Since I'm displaying it in 12 hour format instead of 24 and using the min date if the server time is already PM I can select the time and it outputs it like 05/27/2015 08:50 p -0500
I then do the following code to get the date ready to be inserted into the database:
$date_time = strtotime($_POST['post_date']);
$post_date = date('Y-m-d H:i:s', $date_time);
The problem is strtotime isn't recongining the p for PM and is inserting the date basically 12 hours previously. How can I do this?
Set timeFormat: 'hh:mm tt z'.
And in php use date_create & date_format ( $date_time,'Y-m-d h:i:sA')
See the difference between( strtotime / date_create ).
// method using date_create
//$date=$_POST['post_date'];
$date='05/27/2015 08:50 p -0500';
$date_time=date_create($date);
$post_date=date_format($date_time,'Y-m-d H:i:s');
echo $post_date."<br>";//outputs 2015-05-27 08:50:00
//method using strtotime
$date_time = strtotime($date);
$post_date = date('Y-m-d H:i:s', $date_time);
echo $post_date;//outputs 2015-05-27 13:50:00
strtotime can take many string parameters, may be when you pass the date as '05/27/2015 08:50 p -0500' some conflict is occurring resulting in wrong interpretation of time.
i have used a date picker from bootstrap. I was wondering how would i exclude the 10 days from the selected day the user has clicked
for example the user chooses 10th April 2015. That user can only select dates from 10th April 2015 - 19th April 2015 which would be the ending date.
Here is my code for the date picker.
<script type="text/javascript">
var nowDate = new Date();
var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
$(function () {
$('#startdate').datetimepicker({
minDate:today,
format: 'YYYY-MM-DD HH:MM:SS'
}).change(function (selected) {
var startDate = new Date(selected.date.valueOf());
$('#enddate').datetimepicker('maxDate', startDate+10);
});
$('#enddate').datetimepicker({
minDate:today,
format: 'YYYY-MM-DD HH:MM:SS'
});
});
</script>
Option "enabledDates", disables selection of dates NOT in the array, perhaps that will work for you?
http://eonasdan.github.io/bootstrap-datetimepicker/Options/#enableddates
jQuery code:
<script src="js/jquery-1.10.1.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#book').validate({
rules : {
name : 'required',
sdate : {
required : true,
date : true
},
seats :{
required : true,
number : true,
range:[1,9]
},
},// end rules
}); // end validate()
$('#sdate').datepicker({
minDate : new Date("<?php echo $start;?>"),
maxDate : new Date("<?php echo $end?;>")
});
});
</script>`
Value for date is retrieved from the MySQL database and stored in the variable after converting it to date format. Its passed to the date picker - minDate and maxDate. If I manually pass the date to the minDate and maxDate, the datepicker appears, in the above case even the validation doesn't work nor does the datepicker appear.
PHP code:
$st=$value->getStart();
$start = date("Y-m-d h:i:s", strtotime($st));
$ed=$value->getEnd();
$end = date("Y-m-d h:i:s", strtotime($ed));
HTML code:
Show Date <input name="sdate" type="text" id="sdate" />
Any help on this issue is appreciated. Thanks in advance.
Time in format Y-m-d h:i:s will not with javascript's Date object. Try to use for example this format: D M d Y H:i:s O
$st=$value->getStart();
$start = date("D M d Y H:i:s O", strtotime($st));
$ed=$value->getEnd();
$end = date("D M d Y H:i:s O", strtotime($ed));
I know this question has been asked many times as I have found a few on google and also on stackoverflow.
but none of them explained how to format my datetime in my php so it works in combination with jquery countdown timer. so I am going to ask it here in a hope i get someone shed a light on this for me.
Basically what i am trying to do is to create a countdown timer which will work with mysql datetime.
the datetime is stored in mysql so All need to do is to get the correct format in my php so the countdown timer could work with it.
I am using this plugin: http://keith-wood.name/countdown.html
and here is what i have so far:
PHP formatting:
$end_date = date("m d Y H:i:s T", strtotime($row["end_date"]));
Jquery/Javascript code:
<script type="text/javascript">
$(function(){
var countdown = $('#countdown'),
ts = new Date(<?php echo $end_date * 1000; ?>),
finished = true;
if((new Date()) > ts)
{
finished = false;
}
$('#defaultCountdown').countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds)
{
var message = "";
message += days + " days, ";
message += hours + " hours, ";
message += minutes + " minutes, ";
message += seconds + " seconds ";
message = (finished ? "Countdown finished" : "left untill the New Year");
countdown.html(message);
}
});
});
</script>
when i run this code, all i get is 0 hours, 0 minutes, 0 seconds.
I can only suspect that the issue is from formatting the datetime in my php section!
or am i missing something else as well?
okay I have managed to minify the code to this:
<script type="text/javascript">
$(document).ready(function () {
$('#defaultCountdown').countdown({
until: new Date(<?php echo $end_date; ?>),
compact: true
});
});
</script>
and changed the php to this:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
However, the time shown in the coutdown timer is wrong (way off).
the $end_date is: September 22 2013 23:30:00 GMT in mysql datetime
but the jquery countdown timer is showing:
34d 06:21:48
2013, 9, 22, 23, 30, 00
34days and 6 hours blah blah is absolutely wrong!
what am i doing wrong here now?
The JavaScript Date object is constructed as follows:
Date(year, month, day, hours, minutes, seconds, milliseconds)
That means you probably should be doing something along these lines:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
Sources:
JavaScript Date-object
PHP date-function
EDIT:
In addition, I seem to have found the problem in the jQuery Countdown manual:
A note on Date - the JavaScript Date constructor expects the year,
month, and day as parameters. However, the month ranges from 0 to 11.
To make explicit what date is intended (does a month of 3 mean March
or April?) I specify the month from 1 to 12 and manually subtract the
1. Thus the following denotes 25 December, 2010.
So, you'd have to split the string, substract 1 from the month and rebuild...
$tmp_date = explode(', ', $end_date);
$tmp_date[1] = $tmp_date[1] - 1;
$end_date = implode(', ', $tmp_date);
Link to jsFiddle
my jquery datepicker shows date correctly but the time isn't working. the test result produces the following: 1372046400 = 2013-06-24 04:06:00
as you can see the time isnt same as the systemtime, it should be 8:19pm.
<script type="text/javascript">
$(document).ready(function(){
$("#date").datepicker({
showButtonPanel: true,
minDate: '0M',
maxDate: '+90D',
dateFormat: "d-MM-yy",
onSelect : function(dateText, inst)
{ var epoch = $.datepicker.formatDate('#', $(this).datepicker('getDate')) / 1000;
$('#hidden1').val(epoch);
}
});
});
</script>
php script that displays the output
$dateTime = $_POST['hidden1'];
$date = new DateTime('#'.$dateTime);
echo $saveDate = $date->format('U = Y-m-d h:m:s');
http://php.net/manual/en/function.date.php
it has to be
$date->format('U = "Y-m-d H:i:s');
m is for month
set your time zone (http://php.net/manual/en/function.date-default-timezone-set.php) and then try to use the answer that Sanath has suggested.
Example:-
date_default_timezone_set('America/Los_Angeles');