I am using Mysql database to store the data.
I am facing some issues regarding date fields. Date is stored in YYYY-MM-DD format in database. when i am retrieving date from database I am using the following code.
echo(date("d.m.Y", strtotime($row_getsavedetails['Purchase_WarrantyStartDate'])));
this is working fine if a date is present in the database.
If there is no date in the database '01/01/1970' is getting displayed in the front end.
I am not able to understand, how this date is coming up.
Please help me in this regard.
UNIX timestamps are expressed as seconds relative to Jan. 1st 1970 UTC. strtotime turns a date written in human readable format into UNIX timestamps. If there is no valid date, it returns false. date interprets its second parameter as integer, as UNIX timestamp. Therefore it casts false to 0. 0 is zero seconds from Jan. 1st 1970. Hence, you get 01.01.1970.
Why not check before echoing?
if(!empty($row_getsavedetails['Purchase_WarrantyStartDate']))
{
echo(date("d.m.Y", strtotime($row_getsavedetails['Purchase_WarrantyStartDate'])));
}
else { echo "Date is not available"; }
The fact you get 01/01/1970 , See deceze's answer.
01/01/1970 is the default unix timestamp if left NULL. Best to add a check to handle NULL values in this case.
First of all you have to know that dates are represented (almost anywhere in a computer) like the number of miliseconds from 1/1/1970.
So if you are getting that date it means that the value you inserted in the database (by default) when creating the date was 0.
You are getting the Default date returned if the value you try and convert is null.
You will have to check if the date is empty first.
echo empty($row_getsavedetails['Purchase_WarrantyStartDate']) ? "Date is null" : date("d.m.Y", strtotime($row_getsavedetails['Purchase_WarrantyStartDate']));
Related
Ive recently purchases a script from a website and I was going through the code to see how it inserts and extracts data from mysql... there's this one cell in a table title "issue_date" has just numerical values in it and I am not familiar with this date format, the other column "expiry_date" is the same. Here are the values in the mysql cells "issue_date" [1565481600] and "expiry date" [1568073600]. on the frontend page these numbers translate in to today's (issue) date + 350 days (expiry) date. I need help with identifying the format these numbers are in. Thanks.
It is in timestamp format.
A timestamp is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT).
The value returned by the time function depends on the default time zone.
visit https://www.guru99.com/php-date-functions.html
Check time value of time in human readable form
The format is a Unix Time Stamp.
Here's a good website to convert given Unix Time stamp to human readable format, and back.
Need to know how to deal with a mysql timestamp field ..
I mean When I add a new date into PHPMYADMIN .. and the field is timestamp .. it saves a readable date .I find this strange as I know that timestamp is supposed to be integr .It is the integer that represents number of seconds passed since 01-01-1970 till this date .
And if this is logical .. from my php script what shall I send to the database to save in this mysql timestamp field ?
Thank you so much
Mysql Timestamp format is YYYY-MM-DD HH:MM:SS .And it ranges from 1970-01-01 00:00:01 to 2038-01-19 03:14:07 .For saving unix timestamp in mysql you need to use INT as datatype
TIMESTAMP columns are displayed in the same format as DATETIME columns. In other words, the display width is fixed at 19 characters, and the format is YYYY-MM-DD HH:MM:SS.
Reference: http://dev.mysql.com/doc/refman/4.1/en/timestamp.html
While creating a table, I defined one column of DATE type and one of TIME type. As I insert the values using a php script like :
date--> 2013-11-11
time--> 12:12:12
and when I query the sql browser I see those values in exactly the same manner. But I am unaware of the format with which it stores the date and time. Like yyyy-mm-dd or yyyy-dd-mm.
Is there any way I change it ?
Dates and times are stored in MySQL in the format "YYYY-MM-DD" and "YYYY-MM-DD HH:MM:SS" which is not necessarily the format you want to display in your web page or application. There are two methods to reformat the date and time into the desired format. One is to do it in the SQL query in MySQL calling the DATE_FORMAT() function and the other is to do it with the programming language retrieving the data from the MySQL database.
From MySQL 5.1:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
For second question: you can't change default DATE format for the storage, please see this question also
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format
http://dev.mysql.com/doc/refman/5.5/en/time.html
MySQL retrieves and displays TIME values in 'HH:MM:SS' format
I do not believe this can be changed. But you do not care. You can extract dates and times in the format of your liking with the DATE_FORMAT() and the TIME_FORMAT() functions.
If you want to know the internal storage of Date columns, you can check Date and Time Data Type Representation, but I think you want to select date in different format; which other guys already answered about it.
It is stored in 3 bytes, and it is always YYYY-MM-DD.
The datetime is in Y-m-d H:i:s format, or year month day and hour minute second. If you only use a part, the format stays the same.
If you want to change the format there are many ways. The easiest would be to do something like return date("Y-d-m H:i:s", strtotime($mysqldatetime)); (will turn it to dutch date);
Keep in mind that you are using two seperate columns, one for time and one for the date. If you use only one column the missing values are filled with default values (time would be 00:00:00 and date would be 1970-01-01
I want to ask about changing a datetime value of PHP with datetime value from MySQL data.
I have try to do this at PHP:
$sitgl = date('Y-m-d', strtotime(2012-01-12));
$sijam = date('H:i:s', strtotime(13:00:00));
$awal = $sitgl.' '.$sijam;
$awal2 = date('Y-m-d H:i:s', strtotime($awal));
$debrangkat = strtotime($awal2);
And I'm trying to convert same datetime at MySQL like this (convert it to seconds):
SELECT date_start_book, time_start_book, (TO_DAYS(CAST(date_start_book AS DATE))*86400) + TIME_TO_SEC(CAST(time_start_book AS TIME)) FROM `t_request_queue` WHERE `request_id` = '1301-0087'
which is date_start_book value is 2012-01-12 and time_start_book value is 13:00:00
My question is: why the PHP code return value : 1357970400 but the MySQL value return 63525214800 ?
what must I do to make both of value is same? Is strtotime() not return a seconds or why?
First of all as others have suggested that php code is really hurting brain. You could make that Unix Timestamp in just one line. But to answer your real question. MYSQL TO_DAYS works different than PHP UNIX Timestamp
According to MySQL Website
Given a date date, returns a day number (the number of days since year 0).
mysql> SELECT TO_DAYS(950501);
-> 728779
mysql> SELECT TO_DAYS('2007-10-07');
-> 733321
TO_DAYS() is not intended for use with values that precede the advent of the Gregorian calendar (1582), because it does not take into account the days that were lost when the calendar was changed. For dates before 1582 (and possibly a later year in other locales), results from this function are not reliable
And according to PHP Website timestamp is
Returns the current time measured in the number of seconds since the
Unix Epoch (January 1 1970 00:00:00 GMT).
And hence the difference in two values. Their starting point is way too distant from each other. MySQL starts from year 0 and PHP starts from year 1970.
Suggestion
I would suggest you save php's timestamp in mysql rather than a formatted date time. This will help you stay consistent and allow you to perform any date or time comparisons easily.
Finally, I change the PHP to datetime and at query I'm using ADD_DAYS to add a date with a seconds then I compare it with the PHP datetime result.
So many thanks to all contributor.
In my php application I have this code:
<?php echo date("d/m/ Y ",strtotime($row["m_date"]));?>
In it, $row["m_date"] is fetching from a database.
The problem is that all the dates are printing perfectly except 27/2/2011. It's printing 1/1/1970 instead.
The date in the database is fine, and prints correctly in a PDF.
I'll assume you're getting the date from the database as the string 27/2/2011 because that's most probably what happens (correct me if I'm wrong).
PHP considers the string 27/2/2011 as being in the m/d/Y format, not d/m/Y and tries to parse under that assumption. Because the date is not valid under that format strtotime returns false. Giving false as the timestamp parameter to date is taken as 0, which is the timestamp for January 1st 1970.
What you need to do is either get your date in another format (or better still, as a timestamp) from the database, or parse it yourself (say using explode).
Good luck,
Alin
The database should be able to return the date to you as a UNIX timestamp. For example, MySQL has the UNIX_TIMESTAMP() function.
SELECT UNIX_TIMESTAMP(date_column) FROM table;
Postgres has date_part
SELECT DATE_PART('epoch', date_column) FROM table;
Most other databases should have similar features. If you can get the date out as a UNIX time stamp you can pass that directly to date() without having to use strtotime() as well.
All of this does of course assume you're using a temporal datatype for the columns in question (timestamp, datetime, timestamp with time zone, etc) and not just storing a string. You are using a temporal type, right? If not, then why not?
if you are storing the date in the database as a timestamp this should work
<?php echo date("d/m/Y",$row["m_date"]);?>
if you are storing the date in the database as a date or datetime this should work
<?php echo date("d/m/Y",strtotime($row["m_date"]));?>
How is the m_date stored in the databases? Is it a datetime object? Or a string.
Problem with strtotime is that it isn't real good at deciphering written dates. So something like 27/2/2011 gives problems while 27/02/2011 gives no problems at all.
So there are 2 solutions:
Make sure all the dates that get entered into the database are of the correct format (dd/mm/yyyy).
Write a regular expression that adds a leading zero to all single characters.