i am storing date and time in database using php using gmdate() function in format "Y-m-d H:i:s". for e.g.
2014-03-10 12:35:55
Now,on getting this data into a php variable,for e.g.
$temp=2014-03-10 12:35:55
can i extract and display only the DATE portion excluding the TIME portion??
I am new to date and time manipulation.Any help?
you can get directly the date from database like this
select DATE(column_datetime) as date from yourtable
As Pramod answered,
date('Y-m-d', strtotime($temp));
works.
date('Y-m-d', strtotime($temp));
The above statement returns the date from datetime format
The following code example is adapted by this thread: How to convert date to timestamp in PHP?
There you can also read why it is not safe to rely on strtotime (answer by daremon)
$temp = '2014-03-10 12:35:55';
$a = strptime($temp, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
Edit
From the PHP manual
Note: This function is not implemented on Windows platforms.
Try this:
Echo date('Y-M-D', $var);
Related
I want to fetch old date as well not only current date stored in Timestamp,i used date() function but it only return current date and time.
I think it will work for you
$review_date=$data['review_date'];
$review_date=date('d M,Y', strtotime($review_date));
You can do this using strtotime and DateTime::createFromFormat(). I prefer the second one.
$timestamp = '2018-05-25T10:00:00';
$oDate = DateTime::createFromFormat('Y-m-dTH:i:s');
echo $oDate->format('Y-m-d');
I have searched high and low on how to do this.
I am using the following datepicker script. http://jqueryui.com/datepicker/
Unfortunately it formats the date like so xx/xx/xxxx. However, the date function in mysql saves the date like so xxxx-xx-xx. I used str_replace to replace the / to -. My deli-ma is trying to figure out now how to convert my variable $editsentdate from xx-xx-xxxx to the mysql date format xxxx-xx-xx. When I just send the variable to my DB like it is it stores it as 0000-00-00 which I am sure is b/c that it is not formatted by Ymd. Any help would be appreciated.
$editsentdate = str_replace('/','-',$_POST['edit_sent_date']); // xx-xx-xxxx
Use PHP's function date(). Example: date("Y-m-d", strtotime($_POST['edit_sent_date']))
$editsentdate = date("Y-m-d",strtotime($_POST['edit_sent_date']));
In datepicker, you can format the date
$('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
More info here
There is also a createFromFormat function in PHP. You could initiliaze the date and then format it any way you want.
See PHP - DateTime createFromFormat
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
$mysql_date = preg_replace('~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~',
'$3-$2-$1', $_POST['edit_sent_date']);
Assuming the date format is reliable. Other answers might be more reliable but this is a different way... just FYI.
I've been trying for about two hours to get this working with no luck. I'm trying to convert a date that's entered like 11/18/2012 into a mysql timestamp but everything I've tried just ends up as either 0000-00-00 00:00:00 or NULL in the database :( I'm using PHP + MYSQL so a solution in either would be great
Try This
$release_date=$_POST['release_date'];
echo date("Y-m-d H:i:s",strtotime($release_date));
PHP's DateTime to the rescue!
$datetime = new DateTime($alternativeFormat);
echo $datetime->format('Y-m-d H:i:s'); // mysql format
It's also possible to leave the altering of the data by MySQL, but I advice against it. By using the DateTime object you leave your query open to support other formats aswell.
Use STR_TO_DATE:
SELECT TIMESTAMP(STR_TO_DATE('11/18/2012','%m/%d/%Y'))
http://sqlfiddle.com/#!2/d41d8/4363/0
You stated that you tried:
UNIX_TIMESTAMP(STR_TO_DATE('$release_date','%m/%d/%y'))
The reason this doesn't work is because UNIX_TIMESTAMP's return type is unsigned integer, not TIMESTAMP. This is why you can't insert it into a TIMESTAMP column
Have you tried strtotime(): http://php.net/manual/en/function.strtotime.php
Or exploding on '/' and then using mktime(): http://php.net/manual/en/function.mktime.php
$parts = explode('/', $date);
$timestamp = mktime(0, 0, 0, $parts[0], $parts[1], $parts[2]);
Or any of the other suggestions in answers posted here?
My favorite method:
$date = '10/1/2012';
$mysqlDate = date('Y-m-d', strtotime($date));
I would try something like this.
$sDate = '11/18/2012';
$aDate = explode('/', $sDate);
$sMySQLTimestamp = sprintf(
'%s-%s-%s 00:00:00',
$aDate[2],
$aDate[0],
$aDate[1]
);
var_dump($sMySQLTimestamp);
> string(19) "2012-11-18 00:00:00"
The correct answer will depend upon exactly what you're trying to do, but in most cases it is a combination of these things:
Use a DateTime object, rather than a string, to represent the timestamp in PHP. (Convert it on input rather than when writing to the database).
Use a database interface that has placeholders, and when filling in a value that's a DateTime, automatically converts them to the appropriate string format
This keeps you from having to convert to, or even know, the native format expected by MySQL.
can you enter that example date as 2012/11/18 ?
if yes use
select convert('2012/11/18' ,DATETIME)
or you can use
select convert(str_to_date('11/18/2012','%m/%d/%Y'),DATETIME)
I want to convert MySQL Time data type using PHP and Javascript. I know it can be done using FORMAT_TIME of MySql, but I would like to do the same with php. The Time format is hh:mm:ss by default and I would like to convert it to hh:mm.
Just feed the date into the following function(s):
<?php echo date("h:i", strtotime($date)); ?>
//any date is ok, we care only about the hours and minutes
// $your_date is what comes from db in format hh:mm:ss
$date = new DateTime('2000-01-01 '.$your_date);
echo $date->format('H:i');
I think you could create a DateTime object and then use format("h:i") to get the desired output
Using php I am inserting or updating the mysql database with create date or modified date using the variables
$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$createdate= mdate($datestring, $time);
In this $createdate will be the variable I use to insert or update the table. But it's updating the wrong value. It's not the server time or localtime. Mostly it's 30 mins delay with the server's time.
Use date() function of PHP
$createdate= date('Y-m-d H:i:s');
Edit: after some googling it looks like you're using CodeIgniter. You should have mentioned that in your question.
The format string you're using doesn't match MySQL's date format. You want to use:
$datestring = '%Y-%m-%d %H:%i:%s';
use in mysql query like DATE_FORMAT(purchaseDate, "%Y:%m:%d %h:%i:%s") function