I came across a problem where when I strtotime date it gives me a value which is not equal to what I have in the database.
Example:
In the database I have this value
1398308880
Which is equal to this date
24/04/2014
So what I did is
$date = date('24/04/2014');
$date = strtotime($date);
Which gives me
1398297600
I can't understand, why is there a difference between both of them whereas they should be same value right? what am doing wrong here.
Consider the following:
$time = '1398308880';
echo date("Y-m-d H:i:s", $time);
// outputs 2014-04-23 22:08:00
echo '<br />';
echo mktime(22, 8, 0, 4, 24, 2014);
// outputs 1398395280
The timestamp is still different even when you account for the hour, minute and second. Why? I'm in a Chicago timezone. What timezone are you in? Or more importantly, is your database running in the same timezone as your PHP server?
Related
I have SQLite DB one table contains datetime field
with datatype "timestamp" REAL value is 18696.0
attach image for table structure
So, I want this 18696.0 value to be converted into MySQL Y-m-d format and result should be 2021-03-10
I have didn't found any solution online. any help would be appreciated.
SQLite timestamp converted into MySQL timestamp.
EDIT: Thankyou for updating your question with the correct number and what date it should represent.
You can achieve what you need with a function that adds the days onto the Unix Epoch date:
function realDateToYmd($real, $outputFormat='Y-m-d')
{
$date = new DateTime('1970-01-01');
$date->modify('+' . intval($real) . ' days');
return $date->format($outputFormat);
}
echo realDateToYmd('18696.0');
// returns 2021-03-10
SQLite dates stored in REAL data type stores dates as a Julian Day.
From https://www.sqlite.org/datatype3.html
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
PHP has a jdtogregorian function, in which one comment has a handy function to convert to ISO8601 dates:
function JDtoISO8601($JD) {
if ($JD <= 1721425) $JD += 365;
list($month, $day, $year) = explode('/', jdtogregorian($JD));
return sprintf('%+05d-%02d-%02d', $year, $month, $day);
}
echo JDtoISO8601('17889.0');
// Results in -4664-11-16
The results don't exactly look right, is it definitely 17889.0 in SQLite?
If this float number 18696.0 represents the number of days since 1970-01-01 then the date can also be calculated like this:
$days = 18696.0;
$dt = date_create('#'.((int)($days * 86400)));
$mysqlDate = $dt->format('Y-m-d'); //"2021-03-10"
background information
Or simply with gmdate:
$mySqlDate = gmdate('Y-m-d',$days*86400);
The days are simply converted into seconds to get a valid timestamp for gmdate.
Try this:
<?php
echo date('Y-m-d H:i:s', 17889);
?>
Output:
1970-01-01 04:58:09
I'm using the following function to display time:
date('g:i A', strtotime($time))
If $time is a 4 digit integer, like 1430 the time shows correctly as 2:30 PM. However if the if it's before 10am, say 8am, the time string is 800 and the function shows it as 12:00 am.
I cannot edit time value in my db. How can I fix it on a PHP side?
Thanks.
You need to pad the integer value you are storing in $time with zeroes
Use either of the below functions :
$formatted_time = sprintf("%04d", $time);
OR
$formatted_time = str_pad($time, 4, '0', STR_PAD_LEFT);
Use this value as input to the strtotime function.
Pop an instance of DateTime() and define your timezone and set a timestamp
$date->setTimezone(new DateTimeZone("Asia/Tokyo"))->setTimestamp(800);
3v4l.org/iLk57
I ran time() at 6:38:47 and it returned a different value than strtotime of the same time. Why is this?
It should absolutely return the same value. Run this code:
<?php
$time = time();
$string = date('Y-m-d H:i:s', $time);
$strtotime = strtotime($string);
print "time = $time\n";
print "string = $string\n";
print "strtotime = $strtotime\n";
print "difference = ".($time-$strtotime)."\n";
?>
My output right now:
time = 1377686839
string = 2013-08-28 12:47:19
strtotime = 1377686839
difference = 0
Are you getting a difference with this? You could also post your test code, maybe there's a mistake in there.
strtotime interprets the time string argument according to the system timezone; time is timezone-independent because it just returns the number of seconds since the start of the Unix epoch.
If your system timezone is anything other than UTC you should expect the values to differ, since the time string argument you passed to strtotime was hardcoded. Your notion of "current wall clock time" and that of the system are different, hence the difference in the timestamps.
I need a date object that has a time of 12:00:00am for the current day (meaning no seconds). I am converting that to that to the number of seconds and passing it in another function. It is eventually used for a report filter using date = "someDateHere' off the database, and the hanging seconds in the field are screwing up the report.
I'm not sure what to put in the second parameter in the time function - leaving it blank will use the current time, which is what I do not want. I can't find examples or anything in the php doc. If there is another function that will do the job, I am open to suggestions. This should be simple, but it is alluding me.
date_default_timezone_set('America/Detroit');
$now = date("Y-m-d 0:0:0");
echo $now . '<br/>';
$now = time($now,0);
echo $now . '<br/>';
Thanks in advance.
edit: Please note: I need to convert that date object to seconds. That is where the timestamp is screwing me up with strtotime function and time function. Even though I am passing it a dateobject without a timestamp, converting it into seconds not-so-conveniently is inserting the timestamp as the second parameter which defaults to the current time.
There are lots of available options here, since PHP accepts a wide variety of time formats.
$midnight = strtotime('midnight');
$midnight = strtotime('today');
$midnight = strtotime('12:00am');
$midnight = strtotime('00:00');
// etc.
Or in DateTime form:
$midnight = new DateTime('midnight');
$midnight = new DateTime('today');
$midnight = new DateTime('12:00am');
$midnight = new DateTime('00:00');
// etc.
See time formats and relative formats in the manual for a complete list of formats with descriptions.
Oh, I'd stop using those functions entirely, and start taking advantage of the DateTime class!
$date = new DateTime("now", new DateTimeZone("America/Detroit"));
echo $date->format("Y-m-d");
http://php.net/manual/en/class.datetime.php
time() takes no arguments. what you're doing is pointless. why not just strtotime(date('Y-m-d')) to get the unix timestamp for midnight?
i think mktime() is just what you need http://www.php.net/manual/en/function.mktime.php
<?php
// Set the default timezone to use. Available as of PHP 5.1
date_default_timezone_set('UTC');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
// Prints something like: 2006-04-05T01:02:03+00:00
echo date('c', mktime(1, 2, 3, 4, 5, 2006));
?>
I'm using a PHP script to grab data from Active Directory using LDAP..
When I get the user values for 'lastlogon' I get a number like 129937382382715990
I've tried to figure out how to get the date/time from this but have no idea, can anybody help?
Read this comment on the PHP: LDAP Functions page.
All of them are using "Interval" date/time format with a value that represents the number of 100-nanosecond intervals since January 1, 1601 (UTC, and a value of 0 or 0x7FFFFFFFFFFFFFFF, 9223372036854775807, indicates that the account never expires): https://msdn.microsoft.com/en-us/library/ms675098(v=vs.85).aspx
So if you need to translate it from/to UNIX timestamp you can easily calculate the difference with:
<?php
$datetime1 = new DateTime('1601-01-01');
$datetime2 = new DateTime('1970-01-01');
$interval = $datetime1->diff($datetime2);
echo ($interval->days * 24 * 60 * 60) . " seconds\n";
?>
The difference between both dates is 11644473600 seconds. Don't rely on floating point calculations nor other numbers that probably were calculated badly (including time zone or something similar).
Now you can convert from LDAP field:
<?php
$lastlogon = $info[$i]['lastlogon'][0];
// divide by 10.000.000 to get seconds from 100-nanosecond intervals
$winInterval = round($lastlogon / 10000000);
// substract seconds from 1601-01-01 -> 1970-01-01
$unixTimestamp = ($winInterval - 11644473600);
// show date/time in local time zone
echo date("Y-m-d H:i:s", $unixTimestamp) ."\n";
?>
This is the number 100-nanosecond ticks since 1 January 1601 00:00:00 UT.
System time article in Wikipedia can give you more details.
What about this:
$timeStamp = 129937382382715990;
echo date('Y-m-d H:i:s', $timeStamp);
EDIT ------
I just tried the following and noticed that this method wont work unless the clock on your machine is set 10 years in the future. Below is the code I used to prove the above pretty much useless unless you do more processing maybe..
$time = time();
echo date('Y-m-d H:i:s', $time);
echo "<br />";
$timeStamp = 129937382382715990;
echo date('Y-m-d H:i:s', $timeStamp);
In my case I'm using Pentaho. With a Modified Javascript value you can convert the values, lastLogon is the column I wanna convert from data stream:
calendar = java.util.Calendar.getInstance();
calendar.setTime(new Date("1/1/1601"));
base_1601_time = calendar.getTimeInMillis();
calendar.setTime(new Date("1/1/1970"));
base_1970_time = calendar.getTimeInMillis();
ms_offset = base_1970_time - base_1601_time;
calendar.setTimeInMillis( lastLogon / 10000 - ms_offset); //lastLogon is a column from stream
var converted_AD_time = calendar.getTime(); // now just add this variable 'converted_AD_time' to the 'Fields' as a show in the image below