cI use jQuery calendar date picker on my form. When the date is not filled, it always shows "1969-12-31" value. I did not want to show this value, 0000-00-00 is fine for me.
My MySQL date column is receive_dt DATE NOT NULL,
This is a snippet code from the PHP file to handle the form.
...
$rcv_dt = $_POST['receive_dt'];
list($year,$month,$day)=explode('/',$rcv_dt);
$timestamp=mktime(0,0,0,$year,$month,$day);
$receive_dt=date('Y-m-d',$timestamp);
..., receive_dt) VALUES (....,'$receive_dt')...
I've tried to do the strtotime() but no luck.
$receive_dt=date('Y-m-d', strtotime($rcv_dt));
I've even changed the MySQL reveive_dt column to DATE NULL, but still no luck.
Firstly, the fact that you're getting the end of 1969 rather than the beginning of 1970 (the "Unix epoch" begins at midnight on 1st Jan 1970) suggests you have some timezone-handling bug causing you to "lose" an hour, so just a heads-up on that.
Now, the reason you're seeing this at all, is that PHP's date formatting functions treat whatever input you give them as a number; if you give them an empty string, or null, this will be converted to the number 0, and interpreted as the beginning of the Unix epoch - 1st Jan 1970. MySQL will probably do something similar if you try to pass it an empty string or 0 when populating the column.
What you need to do is specifically detect this case - easy enough if your application should never actually have 1st Jan 1970 as input - and specifically insert a NULL into the database rather than formatting the date.
$invalid_dates='1969-12-31'; // anything before
$rcv_dt = $_POST['receive_dt'];
if(strtotime($invalid_dates) >= strtotime($rcv_dt))
{
$rcv_dt='0000-00-00'; // or $rcv_dt=date('Y-m-d'); // today
}
http://php.net/manual/en/function.checkdate.php
Learn to use the DateTime object. Using strtotime, mktime, and other integer based time formats is an outdated and bad approach.
$dt = date_create($_POST['receive_dt']);
if ($dt !== null)
{
echo $dt->format('Y-m-d H:i:s'); // insert this value
}
if you send $receive_dt as an empty string ..., receive_dt) VALUES (....,'')... (after checking the post variable is empty) then mysql will treat is as a null, otherwise php is sending a date of 0 which for mysql is the start date of the unix epoch.
Related
The dataset value is returning blank, no error on logfile.
$edate = trim($_POST['txtedate']); //user inputs date 12-01-2021
$int_effective_date = new DateTime(strtotime($edate));
echo "edate:- ".$edate."<br />";
echo "strtotime_edate:- ".strtotime($edate)."<br />";
echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));
Result:
edate:- 2021-01-12
strtotime_edate:- 1610389800
dateset:-
To paraphrase #iainn: I'm not 100% sure why you're changing back and forth between DateTime objects and function calls to strtotime?
However, I can explain the most likely issue with your code...
strtotime
Firstly, let's clarify that 12-01-2021 is in the format (d-m-Y)? Hopefully it is, in which case PHPs strtotime function understands it correctly and produces a Unix timestamp (i.e. seconds passed since start of 1970)...
strtotime("12-01-2021");
// Output: 1610409600
// Notes:
// - Possible slight variations based on locale etc.
// - Lookup: date_default_timezone_set
// - This is with "UTC"
DateTime
You then pass that timestamp to DateTime but neglect to inform DateTime what kind of timestamp it is...
$int_effective_date = new DateTime(strtotime($edate));
// Is the same as...
$int_effective_date = new DateTime(1610409600);
However, DateTime doesn't see your timestamp as incorrect and tries to process it anyway...
In the format: HisYmd
But your input is too short for that so it only matches HisY
Time => 16:10
Year => 9600
Given the lack of data DateTime then fills in the blanks with today (example: 2021-02-05)
Day => 05
Month => 02
Which give you a complete timestamp of: 9600-02-05 16:10:40
strtotime from DateTime
Your next line of code then passes that timestamp back into a strtotime call...
echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));
// Is the same as...
echo "dateset:- ".strtotime("9600/02/05");
Now, strtotime will always return something. Which means the first problem is that you're using echo which doesn't output (bool) false.
Try:
var_dump(strtotime("9600/02/05"));
You might ask, why doesn't that happen in the linked code example from #El_Vanja?
Answer
The answer to that, I believe, is that your PHP version is not up to date and anything over the 32 bit date range is going to return (bool) false from strtotime.
To fix this specific problem I suggest you update your PHP version (and OS if you haven't moved to 64 bit!)
However, further to that, I strongly suggest you stick to the DateTime object/class. It saves you from all of these annoying bugs if nothing else...
For reference:
echo strtotime( (new DateTime("#1610409600"))->format("Y-m-d") ); // Output: 1610409600
echo strtotime( (new DateTime("2021-01-12"))->format("Y-m-d") ); // Output: 1610409600
I am using a datepicker within a form which returns the date format F jS, Y (e.g. November 6th, 2013). The mysql database is set up to use Unix Timestamp which is a 10 digit format. I am now looking for a way to pass the form field value (which is November 6th, 2013) in a unix timestamp format into the database, but unfortunately it wont work.
Here is my controller code:
$insertData['enddate'] = $this->input->post('openDays');
I tried the following:
$oldenddtime = $this->input->post('openDays');
$newenddate = int variant_date_to_timestamp ( $oldenddtime );
$insertData['enddate'] = $newenddate;
The db result now is 0... Any idea of what I am doing wrong here?
Once this is resolved, I would need to hour within the timestamp to be ALWAYS 5pm on the selected date...
Thanks in advance ;)
Why not use strtotime? Should do exactly what you need and convert the string to a unix timestamp.
I'm trying to convert an epoch timestamp with php 5.3 with the following statement
date('d-m-Y',strtotime('1349042399999'));
to human readable format and getting wrong result: 01-01-1970what should return30-09-2012. I have been searching around and founding the following topic PHP strtotime returns a 1970 date when date column is null but did not help on my case.
The reason for that is that there are milliseconds embedded in that timestamp, which causes it to go over the integer overflow limit.
chop the last 3 characters, and you're good to go:
$original_timestamp = "1349042399999";
$timestamp = (int) substr($original_timestamp,0,-3);
echo date('d-m-Y',$timestamp);
By using strtotime, you are trying to convert a timestamp into another timestamp. Your timestamp is also in microseconds. The date function just needs the timestamp without the microseconds like so:
date('d-m-Y',substr('1349042399999', 0, -3));
I believe that the Second Rikudo solution worked because of the cast to int, not because of trimming the milliseconds.
It seems to me that this should work for you (it did for me)
$original_timestamp = "1349042399999";
date('d-m-Y', (int) $original_timestamp);
I'm doing a date search filter where I have my date displayed as "j.n.Y G:i (26.6.2012 15:22)".
A user can enter the whole date or only a portion of it: "26.6","6.2012","6","15:22" are all valid inputs. Because I need to check this date in the database the format needs to be changed to the one of the database. For that I use:
$datum = '25.6.2012';
$date = DateTime::createFromFormat('j.n.Y',$datum);
echo $date->format('Y-m-d H:i');
Where I get an error if $datum is not in the format j.n.Y (if I only enter j.n or one of the above mentioned string portions i get an error).
A problem is also, for the entered string 'j.n.Y', i get the right output of the date, which also has the current time added to the date string (which was not in the initial date string). Example: I enter "22.6.2012", then I get the output "2012-06-22 15:33".
Can these two problems get fixed with existing php functions or should I make my own?
Help would be greatly appreciated.
You can list your acceptable data formats in an array, and loop around DateTime::createFromFormat() to see if any of the inputs produce an acceptable date:
$formats = array( 'j.n', 'j.n.Y');
$datum = '25.6.2012'; $date = false;
foreach( $formats as $format) {
$date = DateTime::createFromFormat( $format, $datum);
if( !($date === false)) break;
}
if( $date === false) {
echo "Invalid date!\n";
}
Finally, if you want to get rid of the current time in the newly created object and set the time to 00:00:00, just use the setTime() method on the date object:
// Sets the time to O hours, 0 minutes, 0 seconds
$date->setTime( 0, 0, 0);
For the first problem, you will need to write some code of your own because some of your acceptable inputs are not among the recognized input formats. Normalizing the input value will require you to fully parse it (a regular expression is a good way to start), and then you can call DateTime::createFromFormat without trouble.
For the second problem, putting an exclamation mark ! at the beginning of your format string would fix the time issue. From the documentation:
If format contains the character !, then portions of the generated
time not provided in format, as well as values to the left-hand side
of the !, will be set to corresponding values from the Unix epoch.
The Unix epoch is 1970-01-01 00:00:00 UTC.
However, since you are going to need to fully parse the input as mentioned above the matter is moot. Also note that the exclamation mark would cause missing values for year, month and day to use defaults that are probably undesirable.
My web application consists of library type system where books have due dates.
I have the current date displayed on my page, simply by using this:
date_default_timezone_set('Europe/London');
$date = date;
print $date("d/m/Y");
I have set 'date' as a variable because I'm not sure if it makes a difference when I use it in the IF statement you're about see, on my library books page.
On this page, I am simply outputting the due dates of the books, many have dates which have not yet reached todays date, and others which have dates greater than todays date.
Basically, all I want is the due date to appear bold (or strong), if it has passed todays date (the system displayed date). This is what I have and thought would work:
<?
if ($duedate < $date) {
echo '<td><strong>';
} else {
echo '<td>';
} ?>
<?php echo $date('d/m/Y', $timestamp);?></strong></td>
I have declared $timestamp as a var which converts the date of default MySQL format to a UK version. Can anyone help me out? I thought this would've been very straight forward!
try:
if (strtotime($duedate) < time()) {
// oooh, your book is late!
}
Instead of working with the formatted dates, work with their timestamps. Either convert them back with strtotime() or use time() instead of date. Timestamps can be compared like regular numbers, because that's what they just are.
Okay :) Let's start here:
$date = date; // Wrong!
print $date("d/m/Y");
The above only works because PHP thinks date is a constant. But since you didnt set this constant PHP will convert it to the string 'date'. So $date contains 'date'. Then, when calling $date() as a function, PHP evaluates $date's content, which is 'date' and uses that as the function name, e.g. date(). What you really wanted to do was just $date = date('d/m/y').
Here is how date works:
string date ( string $format [, int $timestamp ] )
First argument is the desired output format, the second argument is an optional timestamp for which the output will be generated. If omitted it will be now. The function returns the output as string.
I assume your $duedates are already formatted strings, e.g. 2010-04-06. So when you do $duedate < $date, you are really doing a string comparison, because both variables hold formatted strings, but not timestamps.
Timestamps on the other hand are just numbers. A timestamp is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). You can get the timestamp for the current date and time with the function time() and you can convert strings that represent dates with strtotime(). So when you want to compare your dates, do
if ( strtotime($duedate) < time() ) { // ... do something
And that's really all there is to it.