This question already has an answer here:
Converting facebook time to human readable time with PHP
(1 answer)
Closed 9 years ago.
I am using time() command in php to store a timestamp value for every time database is updated i.e whenever a value is updated in database a timestamp is added in a time.
For eg: when a change was made to database yesterday night, the value added was 1368132319.
I know the the time() commands returns the no of seconds elapsed from jan 1 1970.
Now what i want to do is convert these no of seconds into a user understandable form which can be displayed on an html page. Like these seconds are converted to date and time.
How do i do that? i cannot think of a logic to implement it. googled it but to no avail
Pretty simple thing, simply use datetime
$date = date_create();
date_timestamp_set($date, 1171502725);
echo date_format($date, 'U = Y-m-d H:i:s') . "\n";
All in the manual http://php.net/manual/en/datetime.settimestamp.php
you can see the demo link....
<?php
$time_in_seconds = 1368132319;
$format = 'Y-m-d H:i:s';
echo date($format,$time_in_seconds);
?>
You can use the date() function. Example:
$timestamp = 1368132319; // in your case the value from the DB
echo date('Y-m-d', $timestamp); // 2013-05-09
echo date('Y-m-d H:i:s', $timestamp); // 2013-05-09 23:45:19
simply you can use below
<?
echo date("D-M-Y",$strtime);
?>
there are various format available for DATE function in PHP you can use required one.
Related
This question already has answers here:
How to get GMT date in yyyy-mm-dd hh:mm:ss in PHP
(6 answers)
Closed 6 years ago.
I am using mssql db with php. Which PHP function can return the current datetime. (i.e.) I want the current date and time to be saved in the following format say for example,
2016-07-04 11:10:05.000
Thanks!
Use date->format http://php.net/manual/it/function.date.php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s.u');
12-hour format
date("Y-m-d h:i:s.u")
24-hour format
date("Y-m-d H:i:s.u")
With "date" you can format the output and with "time" you get the current unix time.
Example
echo date("Y-m-d H:i:s.u", time());
Documentation: http://php.net/manual/it/function.date.php
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I'm using some custom fields in WordPress and I have a custom field called date-of-event which can be used as a date picker from the back-end which is ideal for my client.
Client selects the date in the back-end:
I get the value using:
$date = <?php get_post_meta(get_the_ID(), 'wpcf-date-of-event', true); ?>
However, the date is returned as a single line.
1455100200
How can I convert this single line of data into a readable date and time?
I've looked up http://php.net/manual/en/datetime.createfromformat.php but I'm not too sure what format is being returned in the first place?
It's a Unix Timestamp, so you can get the data by doing the following:
$d = DateTime::createFromFormat('U', '1455100200');
So just replace the number with your variable $date
You can then manipulate the DateTime object as you would like, for storing you probably want the following format:
$d->format('Y-m-d H:i:s');
Example output if you were to print this:
2016-02-10 10:30:00
Hope it helps.
It is Unix timestamp:
1455100200 = Wed, 10 Feb 2016 10:30:00 GMT
The single line you're referring to is what is known as a unix timestamp. It is the number of seconds since Jan 01 1970. (UTC).
To make use of it you can do the following:
$timestamp = get_post_meta(get_the_ID(), 'wpcf-date-of-event', true);
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y-m-d H:i:s');
This question already has answers here:
Does PHP time() return a GMT/UTC Timestamp?
(5 answers)
Closed 7 years ago.
I have an event date & time stored in my database that is being saved as a PHP unix timestamp. Im trying to check if today is greater than the event date and time.
The problem is - i'm trying to check it using local time (America/New_York), but im getting 2 different time zones. time() is displaying in EST and my database is displaying in UTC
Is there any way to check it correctly?
My Event Date from database:
1450447200 (December 18, 2015 2:00pm)
Im trying to compare it with php time()
am I maybe doing this wrong?
*UPDATE - ANSWER*
as per this answer I ended up doing this:
$utc_str = gmdate("M d Y H:i:s", time());
$today = strtotime($utc_str);
$event_datetime = $Event_timestamp;
date_default_timezone_set("America/New_York");
$utc_str_event = gmdate("M d Y H:i:s", $event_datetime);
$event_date = strtotime($utc_str_event);
if($today >= $event_date){
//Do Something
}
You probably have wrong date on server(s). time() always should return epoch (epoch = UTC).
btw. saving date as int on database it isn't best practice
I don't know if this is correct but have you tried using date_default_timezone_set
I'm trying to output the current date and time using this code:
$theDate = date('y-m-d H:m:s', time());
echo $theDate;
And it works fine but the output for time does not change minutes, it simply sites at HH:07:SS, so the minute sits at 07 and the only thing that changes is the seconds and hours.
Is this because of the time function inside PHP? Does it only update minutes so often? Why would it not update the minutes as well?
How can I get an output the same but with minutes showing correctly?
Whenever i run strftime on the server it outputs fine, just trying to figure it out above.
Use i not m:
$theDate = date('y-m-d H:i:s'); echo $theDate;
07 is July :)
m represents months, not minutes. You need to use i for minutes. See the date() manual page for more information.
$theDate = date('y-m-d H:i:s'); echo $theDate;
Your format string is wrong.
It could be: y-m-d H:i:s
You dont need to give date function the second parameter time().
Just try date("format string");
I am making a invoices page and I want the date to appear automatically as an input as well as the date of expiration (30 days later)
is there an automatic function?
I have this:
TD
can value change to something like todaysdate??? or 30 days later??
help! :D
You need to use DateTime::add method.
<?php
$date = new DateTime('2011-01-01');
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
Your question is extremely difficult to decipher.
Perhaps you are looking for PHP's strtotime() function. For example, strtotime('today') returns a timestamp for today's date, and strtotime('next Wednesday') returns a timestamp for next Wednesday.