This question already has answers here:
Calculate age based on date of birth
(11 answers)
Closed 8 years ago.
I want to store a user's date of birth, publish it and show the age. I know how to store the date of birth, but I do not know how I could echo the age afterwards.
I want it like:
username - 06/02/1992 (22 years)
How do I do this?
Essentially you just need to use the date() function to format it.
This answer will help if you're using MySQL and will also describe the basics of formatting dates.
Convert from MySQL datetime to another format with PHP
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I want to insert mm/dd/yyyy to mysql how to convert into default format and I need dynamic query for that
$sql="INSERT INTO user_db (EMAIL,PWD,SQUE,SANS,FNM,LNM,DOB,GENDER,ROLE) values
('".$email."','".$pass."','".$sq."','".$ans."','".$fname."','".$lname."','".???."','".$gen."','".$role."')";
user enter mm/dd/yyyy format convert into default format
There is no way to change what date literals MySQL accepts. They are listed in the documentation here, and are fixed. Instead, you have to change the format of the date you input. Answers to this question will tell you how.
Date format in database is always yyyy-mm-dd format
But While fetching, you can fetch in dd-mm-yyyy format as specified below.
SELECT DATE_FORMAT(datecolumn,'%d-%m-%Y') AS dateColumn FROM table
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Converting a UNIX Timestamp to Formatted Date String
(10 answers)
Closed 7 years ago.
I have a table with a time (int 4) where is stored time ex:1506201203
That would be:
year-mo-da-hh-mm
2015-06-20-12-03
I need a php script to get the time and display it correctly ..
A bit of help ?
You can use from_unixtime.
FROM_UNIXTIME(column_name, '%Y-%m-%d') in your MySQL query.
Or you can just use date function.
date('Y-m-d-h-m', $n['date']).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use DATE FORMAT in my query?
I have a date from form post as 09-23-2012 and in my sql database i save date with datetime format like 2012-09-23 23:11:13
I want to create a query to compare these two date.
These two date should equal.
Try DATE_FORMAT:
WHERE DATE_FORMAT(the_field, '%Y-%m-%d') = CAST(:yourInput AS DATE)
Or something similar
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
PHP - Convert to date format dd/mm/yyyy
mysql saves my date and times like this...
2011-08-31 08:48:40
And when I echo them from the database this is how they appear, how can I get them to a universal format like m/d/y...
Or does it not matter if the mysql row is not a DATE_TIME and just VARCHAR and insert it into the database how I want it??
Thanks...
Do it in PHP:
date("m/d/Y", strtotime('2011-08-31 08:48:40'));
Or do it in MySQL:
SELECT DATE_FORMAT('2011-08-31 08:48:40','%m/%d/%Y')
Of course you won't use hardcoded values in your code. You'll use the column name for MySQL or the retrieved column value, that's probably an element in an array, in PHP.
This post is previously answered:
Convert to date format dd/mm/yyyy
Regards
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 10 years ago.
I am newbe to php. I have small problem to compare dates in php.
I have 4 dates named date1, date2, start_date and end_date in the format yyyy-mm-dd.
i need the functionality like this:
if((date1>=start_date) && (date2<=end_date)){
}
please help me to solve this.
use strtotime to get the timestamps of your dates - this can be easily compared to each other like you already trying it.
If you have PHP 5.3, this is easily handled by the DateTime and DateDiff classes. If you have not, it is much harder, but look at the 'user contributed' section on DateDiff in the online PHP manual.