How to fetch only date from timestamp through PHP? - php

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');

Related

PHP: how to create date before the Epoch (1970) using Date instead of DateTime?

In my PHP script I've got a function handling birthdays like so:
$dateTime = \DateTime::createFromFormat('U', $time);
The problem is that this returns false with negative $time numbers (i.e. dates before 1-1-1970). In the PHP docs there's a comment saying that indeed
Note that the U option does not support negative timestamps (before
1970). You have to use date for that.
I'm unsure of how to use Date to get the same result as DateTime::createFromFormat() gives though. Does anybody have a tip on how to do this?
If you just need to format a UNIX timestamp as a readable date, date is simple to use:
// make sure to date_default_timezome_set() the timezone you want to format it in
echo date('Y-m-d H:i:s', -12345);
If you want to create a DateTime instance from a negative UNIX timestamp, you can use this form of the regular constructor:
$datetime = new DateTime('#-12345');

Get date from database in php

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);

UTC datetime conversion

The values that I received from my device are: 090211 = ddmmyy and 062123 = hhmmss in UTC.
But I found that the time is always 8 hours later if compared to the time that I need. It is because the time for Malaysia is +8:00. First I would like to add 8 hour, and finally I would like to store this kind of date format into my MySQL database as "2011-02-09 06:21:23". How would I convert these values?
To convert in PHP to a datetime, you will need the function DateTime::createFromFormat(); This function will return a DateTime.
Using this function you can also pass the timezone as a parameter.
Example:
$date = DateTime::createFromFormat( 'dmy Gms', '090211 062123', new DateTimeZone("Europe/Amsterdam") );
You can create a output a following:
echo $date->format('Y-m-d H:i:s');
or UNIX timestamp for the MySQL:
echo $date->format('U');
Hope this helps!
PHP has both localtime and gettimeofday functions, are you by chance using the wrong one (or misinterpreting its results)?

How to get the current time in the format `2010-12-20 06:32:48` with PHP?

Anyone knows how to do it?
What I know is time() but it's pure digits..
As you've said, you can get the current timestamp via the time function. You can then pass this timestamp to PHP's date function. (It'll use the current time if no timestamp is provided.)
The specific format you're after is:
date('Y-m-d H:i:s');
To obtain a formatted date/timestamp use the date() function
$dt = date('Y-m-d H:i:s');

Inserting DATE TIMESTAMP Value to MySQL Using PHP

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

Categories