This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Formatting a timestamp
I require a php function that returns the date in the format in the same format as mySQL function CURRENT_TIMESTAMP (i.e like this--> 2011-08-10 17:17:23).
Wondering if there's anything available
echo date("Y-m-d H:i:s");
will do the job.
You can use the date() function for this.
// example result 2010-08-12 22:30:45
$mysqlTime = date ("Y-m-d H:i:s");
Also if you want to get a MySQL date as a timestamp for use in php functions you can use MySQL's UNIX_TIMESTAMP function
Related
This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I'am trying to print my timestamp formated date & time from mysql database in php Codeigniter but this will give me a error message :
A non well formed numeric value encountered
My code:
if ($userTasks) {
foreach ($userTasks as $key) {
echo time_convert($key->t_time);
}
}
public function time_convert($timestamp){
return date('l Y/m/d H:i', $timestamp);
}
There are codeigniter DateTime library and helper as well but I prefer using normal PHP for this basic functionalities.
Basic two ways:
Convert your mysql timestamp to Unix Timestamp using "strtotime()" and use "date()" function to convert unix timestamp to formatted date.
$unix_ts=strtotime('2020-07-01 20:46:23');
echo date("Y-m-d",$unix_ts);
Create date object from mysql timestamp using "create_date()" and use "date_format()" function which takes date object as its parameter.
$date = date_create('2020-07-01 20:46:23');
echo date_format($date, 'Y-m-d');
You can use either of two ways. First is DATE FORMAT function in your SQL, like this.
$this->db->select(' DATE_FORMAT(orderdate, '%Y-%m-%d') as orderDate')
To learn more about it Read here
Another way to it is through PHP
you can use PHP's DateTime class like this
public function time_convert($timestamp){
// Create datetime object
phpDatetime = DateTime::createFromFormat ("Y-m-d H:i:s", $timestamp);
// return datetime in your required format
return phpDatetime->format('l Y/m/d H:i');
}
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am taking the input in M-d-Y format from calender but I want to convert it into Y-m-d when it store in the database. What method can I use?
You can try this. strtotime is your friend.
echo date("Y-m-d", strtotime('Dec-02-2014'));
or mysql DATE_FORMAT function, but mysql stores dates in yyyy-mm-dd format.
I think it better to do it in Mysql itself. When we have so many function. Like below:
SELECT STR_TO_DATE('Jan-31-2014', '%b-%d-%Y');
You can use it for insert query like below:
Insert into tableName `date` = STR_TO_DATE(InputVal, '%b-%d-%Y');
For more info:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Try this:
$date="05-25-2014";
print(date("y-m-d",strtotime($date)));
use the date() and strtotime() like:
$var = date('Y-m-d', strtotime($timestring))
Converting string to Date and DateTime
copy from: https://stackoverflow.com/a/6239010/2478144
Make not that there is a difference between using forward slash / and hyphen - in the strtotime function. to quote from php.net
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Covert time format in php
I am getting a start_time field value from mysql data base as 2012-08-14 21:30:00
and i want to convert it in php format like 2012-08-14T09:30 is there any method to do
this in php ?
use strtotime()
$date = strtotime('2012-08-14 21:30:00');
echo date('Y-m-d\Th:i',$date);
see this example.for the required date & time format:
$date=date("Y-m-d H:i:s");//get the current date
$d=strtotime($date);//convert in strtotime
echo $final_date=date("Y-m-d\Th:i:s",$d);//in the first argument of date ,put the format whatever you want,but be sure to convert it in strtotime first.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
Here is unix timestamp of today
1306702800
How to convert it to look like 30-05-2011 using php ?
You can use the date function to convert (and arbitrarily re-format) timestamps as such:
echo date('d-m-Y', 1306702800);
Additionally, you can get the current timestamp via the time function, so you could output the current date via: echo date('d-m-Y', time());.
Use PHP's date functions: http://nl3.php.net/manual/en/function.date.php
See date
echo date('d-m-Y', '%your-time-stamp%');