echo strtotime('2017-05-03 16:16:01');
echo "<br>";
echo time();
I ran this query at 2017-05-03 16:28:01 so output of time() should be higher and strtotime('2017-05-03 16:16:01'); should be lower but I am not getting the output.
above code prints
1493828161
1493809172 //
But I expected it to print if I run the query at 2017-05-03 16:02:01 same timestamp
1493809172
1493809172
But it prints with big difference.
time() return dynamic current timestamp which will change every moment.
Where as your passed date is static date, which will convert it into its timestamp() value and will show it.
Conclusion : time() is that which can not be stopped, it run as time pass by whereas timestamp with given date time, it will always show static value timestamp of given date time.
strtotime('2017-05-01 16:02:01')
Convert provided time into string to time
time ()
That convert to Returns the current time as a Unix timestamp
Related
I'm trying to echo out the current system date. But it outputs the date of tomorrow. I double check the System Date and tested changing the date. Still it adds 1 extra day when echoing out.
What could be the problem?
<?php
echo date("Y-m-d");
?>
If not specified, 2nd argument for date() defaults to current Unix timestamp - amount of seconds since 01.01.1970, 0:00 GMT.
You can use this to set your timezone, or manually convert the date to correct one: date($format, time() + $difference).
Hello i need to get this date value from php 1328569380 and convert it to javascript date.
By the way how is this date "1328569380" type of form called ?
The numeric date time your are referring to is called a timestamp. It is the number of seconds elapsed since january 1st of 1970 if i'm not wrong.
To send a date to javascript, just print it out using the timestamp x 1000 since it also accepts millisecond initialization format:
mydate = new Date(<?php echo $mytimestamp*1000; ?>);
Good luck
This is a Unix epoch timestamp. See the following thread for the how-to:
Convert a Unix timestamp to time in JavaScript
Your value is the number of seconds that has passed since 1970-01-01 00:00:00, called the Unix epoch.
JavaScript counts the number of milliseconds instead, thus you have to multiply your timestamp with 1000 prior to using it to create a JavaScript date-object.
var phptimestamp = 1328569380;
var date = new Date(phptimestamp * 1000);
The output of the following program can be seen here: http://codepad.org/egNGJBUL
<?php
/* Checking if time() is really timezone independent */
date_default_timezone_set('UTC');
echo time();
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo time();
echo "\n";
/* Using date() function passing timestamp parameter */
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s',time());
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo date('Y-m-d H:i:s',time());
echo "\n";
/* Using date() function without passing timestamp parameter */
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s');
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo date('Y-m-d H:i:s');
echo "\n";
From line 1-2 of the output, we can see time() returns a value which is really timezone independent.
In line 3-4, it's strange that date() function ignores the timestamp parameter and still display the date time according to the timezone set.
Why is it like this?
Not really sure what you are expecting to see, but yes, looks very normal to me.
A timestamp is a integer counted from a certain point in time (usually the UNIX EPOCH). While the display of this value is timezone independent, it is no more or less so that say, the value of a properly formatted date, notated with a timezone, is timezone independent...
example, all of the following statements are both true (logically)
1297799809 == 1297799809
2011-02-15 19:56:49 (UTC) == 2011-02-16 05:56:49 (Austria/Queensland)
All time is 'timezone independant'. Timezones only affect the way we display a particular moment in time.
date() functions second parameter, if not specified, is time() value.
date() Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
from date()'s manual
So actually nothing is being ingnored.
The date function returns the date of a timestamp calculated for the current timezone, as others have said, if no timestamp is passed to it, then the current time is used for the timestamp, so passing time() is the same as not passing anything at all.
However, doing something like $time = time();sleep 5;echo date($format,$time); will get you a date 5 seconds in the past.
It's meant to display the date formatted for current timezone so you can have a universal method of keeping time that's constant across computers/servers and be easily parsable, and yet be able to display the date in any timezone desired.
The UTC timezone is actually the time that the timestamp is calculated to, more precisely, the number of seconds since 00:00 Jan 1, 1970 UTC, then it adds or subtracts 3600 (60*60) seconds from/to the timestamp per hour offset from UTC time to get the time in the currently set timezone.
I have a mySQL database with a timestamp field. It currently only has one entry while I'm testing, it is
2010-02-20 13:14:09
I am pulling from the database and using
echo date("m-d-Y",$r['newsDate'])
My end result is showing as
12-31-69
Anyone know why?
Edit:
editedit:
disregard that edit... the FTP addon for notepad++ timed out and unfortunately doesn't display an error when it can't synch.
The date function expects an UNIX timestamp as its second parameter -- which means you have to convert the date you get from the DB to an UNIX timestamp, which can be done using strtotime :
$db = '2010-02-20 13:14:09';
$timestamp = strtotime($db);
echo date("m-d-Y", $timestamp);
And you'll get :
02-20-2010
You were passing the '2010-02-20 13:14:09' string to the date function ; that string is not a valid UNIX Timestamp.
'12-31-69' is probably 1970-01-01, in your locale ; and 1970-01-01 is the Epoch -- the date that corresponds to the 0 UNIX Timestamp.
For starters, the php date() function is expecting seconds as the second variable. So that accounts for why your date is displaying wrong. Check this source on that issue.
Which then provides us the answer to the problem, to get PHP to format the date from a SQL timestamp correctly, we just change the query a tad...
SELECT author, `when`
Change it to...
SELECT author, UNIX_TIMESTAMP(`when`)
Then use the PHP date function, with the variable that is storing the result of that above SQL query.
You could just use MySQL's date_format() function instead:
SELECT date_format(timestampfield, '%m-%d-%Y') FROM table etc....
This will save you having to round-trip your timestamp into unix time and then back into a normal date string in PHP. One datetime formatting call rather than two.
i think this will be useful to newble:
example basic subtraction 1 hour from date from MYSQL format:
$to='2013-25-10 22:56:00'; //curr time
$timestamp = strtotime($to); //convert to Unix timestamp
$timestamp = $timestamp-3600; //subtract 1 hour (3600 this is 1 hour in seconds)
echo date("Y-m-d H:i:s",$timestamp); //show new date
EDIT: After checking, it appears that MySQL returns a timestamp as a string to PHP, so this answer was bogus :)
Anyway, the reason you get a date in 1969 is probably that you're converting a zero unix time from UTC to localtime. The unix time is the number of seconds since 1970. So a value of 0 means 1970. You probaby live in a timezone with a negative offset, like GMT-6, which ends up being 31-12-69.
ok, I was wrestling with this for a week (longer but i took a break from it).
I have two specific fields in tables
creationDate > timestamp > current_timestamp
editDate > timestamp > current_timestamp
they were pulling out either dec 31 1969, or just nothing... annoying... very annoying
in mysql query i did:
unix_timestamp(creationDate) AS creationDate
unix_timestamp(editDate) AS editDate
in php convert i did:
$timestamp = $result_ar['creationDate'];
$creationDate = date("Y-M-d (g:i:s a)", $timestamp)
echo($creationDate);
$editstamp = $result_ar['editDate'];
$editDate = date("Y-M-d (g:i:s a)", $editstamp)
echo($editDate);
this solved my problem for me returning
2010-Jun-28 (5:33:39 pm)
2010-Jun-28 (12:09:46 pm)
respectively.
I hope this helps someone out..
I have noticed that regardless of a given script's execution time, every date() call will return the same timestamp regardless of where the function is called within the script. It looks like it just returns the time at which the script first started executing.
For logging purposes, it would be extremely useful to be able to get incremental timestamps from within a script. Is this possible? Is there a way to do this that is relatively lightweight?
Edit: Would the example for the microtime() function suggests it might do this. Can anyone confirm?
Update: microtime() does work, but I cannot format it with the date() function because date() only accepts timestamps as integers (so no microseconds). How can I get a properly formatted date from the value returned by microtime() ?
First of all, date() is used to format a timestamp - it's just the default behaviour that it'll use the current timestamp, when date() is called without second parameter. The timestamp used will be the timestamp the moment the function is called - calling date('Y-m-d') is the same as calling date('Y-m-d', time()) where time() will give you the
[...] the current time measured in the
number of seconds since the Unix Epoch
(January 1 1970 00:00:00 GMT).
You only get the same timestamp every time you call date() because your script is too fast and runs within one second resulting in no timestamp-change.
To address your second problem of formatting the microtime() return value, you can do the following (untested)
function formatTime($microtime, $format)
{
list($timestamp, $fraction) = explode('.', $microtime);
return date($format, (int)$timestamp) . '.' . $fraction;
}
The value given to $microtime should be a float, which you get when you pass true as a parameter to microtime()
I ran this code on my machine:
<?php
$time_start = time();
sleep(2);
$time_end = time();
print 'Start: ' . date("m/d/Y # g:i:sA", $time_start) . '<br>';
print 'End: ' . date("m/d/Y # g:i:sA", $time_end);
?>
And it output:
Start: 10/23/2008 # 3:12:23PM
End: 10/23/2008 # 3:12:25PM
Leading me to believe time() does not just return the time when execution started.
http://us.php.net/microtime gives me different times within the same script.
You can use the Pear Benchmarking package for getting timing and profiling information.