Date field is being reset in PHP and MySQL - php

When I am trying to edit some fields in the form, the date is being reset to 01-01-1970 01:00:00 but its stores and retrieves its value from the database perfectly for the very first time. Then it's being reset when I edit some other fields except date.
Below my jQuery date picker:
<script>
$(document).ready(function(){
$("#policy_start_date").datepicker({
numberOfMonths: 1,
onSelect: function(selected) {
$("#policy_end_date").datepicker("option","minDate", selected)
}
});
$("#policy_end_date").datepicker({
numberOfMonths: 1,
onSelect: function(selected) {
$("#policy_start_date").datepicker("option","maxDate", selected)
}
});
});
</script>
POST DATA
$date_t=$_POST['policy_start_date'];
$datearr=explode('/',$date_t);
$month=$datearr[0];
$day=$datearr[1];
$year=$datearr[2];
$policy_start_date=$year."-".$month."-".$day;
//$policy_end_date = $_POST['policy_end_date'];
$date_t=$_POST['policy_end_date'];
$datearr=explode('/',$date_t);
$month=$datearr[0];
$day=$datearr[1];
$year=$datearr[2];
$policy_end_date=$year."-".$month."-".$day;

Usually this is happen because the format stored in db and the format sent/acceptable by datepicker.
Standard datepicker format is 09/03/2013 (m/d/y).
What is displayed on the textbox when you echoed the data?
1. 09/03/2013 (m/d/y)
or
2. 2013-09-13 00:00:00 (Y-m-d H:i:s)
if option one (1) is displayed, then we certain need to see the code that display the form you used.
if option two(2) is displayed, then you need to format it before echoing into the textbox.
use this,
$time = strtotime($date_stored_in_database);
$your_date = date('Y/m/d',$time);
The strtotime will convert the date you stored in database into UNIX TIMESTAMP, and date function will convert it to the format you needed.

Related

php how to post date range picker with 2 variables

I'm using date range picker
<input class="form-control input-daterange-datepicker" type="text" name="daterange" value="01/01/2015 - 31/01/2015" />
When I post the form i get 1 value for this range.
is it possible to get 2 values - from and till (when i post)?
<!-- Date range Plugin JavaScript -->
<script src="vendors/bootstrap-daterangepicker/daterangepicker.js"></script>
<script>
// Daterange picker
$('.input-daterange-datepicker').daterangepicker({
buttonClasses: ['btn', 'btn-sm'],
format: 'DD/MM/YYYY',
minDate: '06/01/2015',
maxDate: '06/30/2025',
applyClass: 'btn-danger',
cancelClass: 'btn-inverse',
dateLimit: {
days: 45
}
});
</script>
I think that this is the right project -
https://github.com/dangrossman/daterangepicker
The widget will post both the dates as one string. Since you named your input daterange you can access the result in PHP using:
$aRanges = explode(' - ', $_POST['daterange']);
var_dump($aRanges);

Formatting database date to AM/PM time (vue)

I'm receiving dates from my database in the format 2018-08-08 15:38:48 however I want it to show 3:38 pm instead.
I'm just unsure when to make this change, can I change it while it is being posted? They are dates of messages being sent to a person.
Current code:
<div v-for="messages in userMessages">
<div class="date">
{{ user.created_at }}
</div>
</div>
Output:
2018-08-08 15:38:48
How do I transform the date in vue? (when it is in a v-for?)
Vue does not offer date formatting. You will need your own filter to format the date. Or you use a package like the following: https://github.com/brockpetrie/vue-moment#readme
<span>{{ someDate | moment("hh:mm a") }}</span>
Vue.js does not natively allow dates to be formatted in a different way.
I suggest you to use more famous libraries like moment.js to format your dates however you want.
For example:
import moment from 'moment'
Vue.filter('formatDate', function(value) {
if (value) {
return moment(String(value)).format('MM/DD/YYYY hh:mm')
}
}
https://momentjs.com/
How do you use it?
You can use like this.
moment('2018-08-08 15:38:48').format('LTS'); // 03:38:48 PM
Your datetime format is not the ISO standard we would like, but we'll live with it... When handling dates, it's a good practice to convert them to date objects as soon as you receive them. If they're arriving in JSON, you can do this with a reviver function as the second argument to JSON.parse(). Here is a reviver function that will do the job for you...
Then,at the point you need to display, you can format your time with toLocaleTimeString()
function nReviver(key, value) {
var a;
if (typeof value === "string") {
a = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d*)?$/.exec(value);
if (a) {
return new Date(a[0]);
}
}
return value;
}
// do this when you receive your data from the server and all
// dates, wherever they are in the JSON, will be converted to date objects
var myObj = JSON.parse('{"myDate":"2018-08-08 15:38:48"}',nReviver)
// ...then, when you want to display, format with toLocaleTimeString()
console.log(
myObj.myDate.toLocaleTimeString({},{
hour12:true,
hour:'numeric',
minute:'numeric'
})
.toLowerCase()
)

Disable time picker in Datetime picker

I want to disable the time picker in datetime picker.I am using some parameters like pickTime: false and format: "dd MM yyyy" .But no use..i'm using from this http://eonasdan.github.io/bootstrap-datetimepicker/
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
format: "dd MM yyyy"
});
</script>
Plzz give a solution
Better to use this to get only Date and you also have multiple Options with it
Have a look at this
Bootstrap DatePicker
You can pick date with
$('#datetimepicker1').val()
or
$('#datetimepicker1').data('date')
This only gives the date value
Possible Duplicate of How to get the value of the date using Bootstrap Datepicker
You did some wrong in object passing into the function.
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker({
format: "dd MM yyyy"
});
});
</script>
It was your format string. 'yyyy' does not exist. It is 'YYYY' instead.
Also, dd would have gotten you the days like this: Su, Mo, Tu, ...
You probably wanted 01, 02, 03,...
Here's the documentation on the format:
http://momentjs.com/docs/#/displaying/format/
Possible solution:
$('#datetimepicker4').datetimepicker({
format: 'YYYY-MM-DD'
});
As answered by Celt in this post:
https://stackoverflow.com/a/28542910/2295862

Retrieving and Comparing Dates using JQuery And PHP

I am using jQuery's datepicker in wordpress/PHP and I am using this code to show dates:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {
$("#datepicker").datepicker();
$("#datepicker").datepicker("option","dateFormat",'yy-mm-dd');
});
</script>
Date<input type="text" id="datepicker" size="20" maxlength="15" style="width: 80px;" readonly="readonly"/>
It successfully displays my date.
Also, I have a table (wp_redeem_vouchers) in the database that has specific voucher redemption dates saved in it:
id wp_deal_id redeem_date
1 Amazon6767 2012-03-21
2 Ebay87nm 2013-06-12
3 M&Svouc 2013-01-01
4 ASDA 2011-09-08
Question:
How can I retrieve "redeem_date" and compare with the datepicker's textbox date (upon selection) with the date already saved in the database?
Is there anyway I could disable past date so that user can't choose the past date from date picker? i.e The user on 14/03/2013 can't choose 22/02/2013 from datepicker.
For your i)
$('#datepicker').datepicker( {
minDate: 0,
onSelect: function(dateText, inst) {
$.ajax({
type: "POST",
url: "some.php",
data: { date: dateText },
onSuccess: function(e){
alert("success");
}
});
});
Then in the some.php file you do your compare with mysql and you return a success or failure getting the alert(success).
Then I would start with
$date = $_POST['date'];
die($date);
In the some.php just to see if it is passed correctly.
Well, you can pass your datepicker date string (adjust format to $("#datepicker").datepicker("option","dateFormat",'yyyy-mm-dd');) to following JavaScript function which creates a Date object:
function createDate(s) {
var bits = s.split('-');
var d = new Date(bits[0], bits[1] - 1, bits[2]);
return d;
}
Then you have to retrieve the date value from your database.
I do not know if there is a wordpress limitation but you can trigger a Ajax-Request which reads out the database value and give you as a response the date string as a JavaScript variable.
This variable can also be passed to the createDate(date) function. With these two date objects you can call easily compare the date with the time in milliseconds -> getTime() function of date object.
Regards

JQuery and PHP Date difference

I am using a jquery date picker and also setting a date through php date('Y-m-D') functions. Both of them give different date for today. Jquery picker fills the field with the date that is one day ahead of php date(). Here is the function for jquery. I need jquery to show same date for today as php.
<script type="text/javascript" charset="utf-8">
var $j = jQuery.noConflict();
$j(function()
{
// initialise the "Select date" link
$j('#date-pick')
.datePicker(
// associate the link with a date picker
{
createButton:false,
startDate: '01/01/1970',
endDate: (new Date()).asString()
//endDate:<?php echo date('y-m-d'); ?>
}
).bind(
// when the link is clicked display the date picker
'click',
function()
{
updateSelects($j(this).dpGetSelected()[0]);
$j(this).dpDisplay();
return false;
}
).bind(
// when a date is selected update the SELECTs
'dateSelected',
function(e, selectedDate, $td, state)
{
updateSelects(selectedDate);
}
).bind(
'dpClosed',
function(e, selected)
{
updateSelects(selected[0]);
}
).val(new Date().asString()).trigger('change');
var updateSelects = function (selectedDate)
{
var selectedDate = new Date(selectedDate);
if(selectedDate != "Invalid Date")
{
$j('#d').val(selectedDate.getDate());
$j('#m').val(selectedDate.getMonth()+1);
$j('#y').val(selectedDate.getFullYear());
}
}
// listen for when the selects are changed and update the picker
// default the position of the selects to today
var today = new Date();
updateSelects(today.getTime());
});
</script>
jQuery uses the client computer's date while php uses the server's date. They're basically different if you haven't set the default timezone in php. Take a look at this:
http://php.net/manual/en/function.date-default-timezone-set.php
You can set the default timezone in php.ini file located on the PHP main directory (Eg. PHP5.4)
As for the using of the server's date in the datepicker:
A quick google search lead me to this: how to display server side dates in jquery datepicker?
basically what they have done is creating a new date based on the current timestamp:
$timestamp = strtotime("2012-08-02");
minDate: new Date('<?php echo $timestamp; ?>');
DO NOT TRUST THE CLIENTS DATE AND TIME
These values can be altered at a whim.
Just use them on advisement.
Besides Javascript is there to enhance the users experience (i.e. make it more interactive). But at the end of the day you will have to pick up the pieces it you do not validate and verify the data you get from the client

Categories