i have a script that saves a date like this.
$date = date('i')+50;
When i store this on my database, i got something like 1382851302 wich works great to compare for past and future just using > or <.
In other part of the script for the admin im using a filter based on jquery datepicker.. the problem is i cant convert the date selected there to the same "date()" format so i can compare the date , i think time is somehow envolved here (i) and well i cant.
here is my bad try.
$iniciopub = date('Y-m-d',strtotime($_POST['iniciopub']));
$finpub = date('Y-m-d',strtotime($_POST['finpub']));
This is not working.. (it just store "1970" )
if i try this..
$iniciopub = date($_POST['iniciopub']);
$finpub = date($_POST['finpub']);
I just save the day , i think because the format is like 00/00/0000 so the / is "cutting" the value cause i can just save int. in the database as you can see in the format i need, i just nned numbers..
Really lost on this, sorry if this is a fool question.
Im studing so dont be so hard. EDIT:
Ok this is my code now im using mktime()
$iniciodate = str_replace("/",",",$_POST['iniciopub']);
$iniciopub = mktime(0,0,0,$iniciodate);
$findate = str_replace("/",",",$_POST['finpub']);
$finpub = mktime(0,0,0,$findate);
This saves this to timestamps for 10/27/2013 = 1382824800 but the same for 10/31/2013
ALso im using this format, i have not problem with it
dateFormat: 'mm/dd/yy',
This is now set in datepicker jquery ui
Your variable $iniciopub
has value 1970-01-01
because strtotime()
returned false
:
var_dump( strtotime($_POST['iniciopub']) );
Take a look at supported date and time formats. If you are saying that you have format like 00/00/0000
in $_POST
, then strtotime asumes this is american month, day and year (mm/dd/yyyy
), like it states here.
If inputed format 00/00/0000
is dd/mm/yyyy
then the easiest method would be to use DateTime::createFromFormat
or str_replace('/', '-', 'dd/mm/yyyy');
to make it european date format dd-mm-yyyy
.
Question update
This code is just wrong:
$iniciodate = str_replace("/",",",$_POST['iniciopub']);
$iniciopub = mktime(0,0,0,$iniciodate);
mktime()
has 6 parameters (7th is is_dst
wthich doesn't matter now). You cannot use $iniciodate
like that, because you are inputing only 4th parameter into mktime()
function, and not 4th, 5th and 6th as you might think.
mktime(0,0,0,'10,27,2013');
is not the same as mktime(0,0,0,10,27,2013);
or `mktime(0,0,0,'10','27','2013');
. The reason why date 10/27/2013
and 10/31/2013
return same timestamp is because in both cases, when you cast string 10,27,2013
and 10,31,2013
to integer (4th parameter), you get 10
. See:
$v = '10,27,2013'; var_dump( (int)$v ); # int(10)
$v = '10,31,2013'; var_dump( (int)$v ); # int(10)
And because of that, your call is the same if you would call mktime()
like:
var_dump( mktime(0,0,0,10) ); # int(1382832000)
If you would have error_reporting on (put error_reporting(-1);
on the beggining of the php file), you would see, that after calling mktime(0,0,0,$iniciodate)
you would get NOTICE:
Notice: A non well formed numeric value encountered in file.php on line XX
Since 10/27/2013
and 10/31/2013
are standard american date formats, you can just use strtotime()
instead of mktime()
to convert date to timestamps:
var_dump( strtotime('10/27/2013') ); # int(1382832000)
var_dump( strtotime('10/31/2013') ); # int(1383177600)
You asked in the comments: if i store a date as dd-mm-yyyy. how can i compare the date with other date. am i doing fine storing as timestamp?
Yes, you can store them as timestamp. But I haven't used timestamp in my code for ages, I am storing dates as YYYY-MM-DD; in RDBMS this is kinda best practice.
Better way not conver to mktime in PHP - save data as mktime already:
function mdf_init_calendars() {
jQuery(".mdf_calendar").datepicker(
{
showWeek: true,
firstDay: 1,
changeMonth: true,
changeYear: true,
showButtonPanel: true,
onSelect: function(dateText, self) {
var date = new Date(parseInt(self.currentYear, 10), parseInt(self.currentMonth, 10), parseInt(self.currentDay, 10), 23, 59, 59);
var mktime = (date.getTime() / 1000);
jQuery(this).prev('input[type=hidden]').val(mktime);
}
}
);
jQuery(".mdf_calendar").datepicker("option", "dateFormat", 'dd-mm-yy');
jQuery(".mdf_calendar").datepicker("option", "showAnim", 'fadeIn');
//+++
jQuery(".mdf_calendar").each(function() {
var mktime=jQuery(this).prev('input[type=hidden]').val();
var date = new Date(mktime*1000);
jQuery(this).datepicker('setDate', new Date(date));
});
}
You saves data in hidden inputs, and load in them when page loading, so in PHP code you get mktime always and do not need to think about data format, you can set it in js as you want (in example inline) PHP: