I'm saving to my database in the column 'time' in this format (user input using datetimepicker):
dateFormat: 'mm-dd-yy',
timeFormat: 'hh:mm tt',
Example: 07-31-2014 03:37 am
I also have a column named 'status'. If the 'time' (the time saved in the column) + 24 hours surpasses the current time() then 'status' should = 'Expired'. Below is my attempt, but I think strtotime is not interpreting my timestamp as it's marking everything as Expired... example: 08-19-2014 02:05 am Expired
if( (strtotime($row['time'] . "+1 days")) < time()) { $row['status'] = 'Expired'; }
Assuming you are getting date from database like in this format "07-31-2014 03:37 am".
You need to convert it and format it as strtotime readable one like below.
list($month,$day,$year,$hour,$minute) = preg_split("/[-\s:]+/", $row['time']);
$row['time'] = $year."-".$month."-".$day." ".$hour.":".$minute.":00";
Try it.
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 am trying to check if one date is equal than the other date, but I can't get the match because the date format coming from the form turns into a different order once it gets through the "parse" code.
I need to format this date to find the match, here is a sample code to show how I am trying:
...
// $ago will give me this date: 2016-12-09 00:00:00
$ago = Carbon\Carbon::today()->addDays(2); // Todays date + 2 days
//$request->datex has the date coming from a form with this format, '12-06-2016'.
// Once a parse $request->datex here, the date gets out of order:
$my_date = Carbon\Carbon::parse($request->datex);
// it shows the date like this, 2016-09-12 00:00:00 , I need it to be on this format: 2016-12-09 00:00:00
// then I could do this:
if ( $ago$ == $my_date ) {
dd($my_date.' is equal to: '.$ago );
}else{
dd(' Not equal!');
}
...
Thanks for looking!
Change this line
$my_date = Carbon\Carbon::parse($request->datex);
with this:
$my_date = Carbon::createFromFormat('m-d-Y', $request->datex)
I've assumed that your format '12-06-2016' means DAY-MONTH-YEAR
UPDATE
Tested my solution on my machine and it works, date is recognized properly:
When
$request->datex = '12-06-2016'
then
$my_date = \Carbon\Carbon::createFromFormat('m-d-Y', $datex);
gives me date like that: public 'date' => string '2016-12-06 18:52:09.000000' (length=26)
Date has been parsed properly. The thing that I've assumed just now. These dates won't be same cause of hours, minutes, seconds and milliseconds. To fix that just we have to compare dates that way:
if ( $ago->format('Y-m-d') == $my_date->format('Y-m-d') )
//do something awesome with our equal dates
PHP expects DD-MM-YYYY or MM/DD/YYYY formats.
If you always have a MM-DD-YYYY format, you could do this before parsing:
$request->datex = str_replace('-', '/', $request->datex);
I want to convert this date ( 02-12-2010) mm-dd-yyyy to time format
ie
to 02-12-2010 0 hours 0 minutes and 0 seconds
i have user time and date functions but when i refresh the page its value is changing as per the time.
i need that to be fixed
also i want to convert this date (01-24-2009) to time format.
please help me
Thanks
Use the strtotime function to convert an existing date/time string into a timestamp for the date function.
$new_date = date('m-d-Y h:i:s', strtotime('02-12-2010'));
The reason that your date keeps updating with the current time is that the date() function uses the current system's timestamp by default when no second parameter is provided.
Use the date_parse_from_format () API function to transform your given format to an array. E.g.
$date = "02-12-2010";
$dateArr = date_parse_from_format("d-m-Y", $date);
Output it with something like:
$output = $dateArr['day'] . '-' . $dateArr['month'] '-' . $dateArr['year'];
You can use the strtotime function eg:
$mydate = date('m-d-Y h:i:s', strtotime('02-12-2010'));
I have this to select a date from a mysql db, and compare it to an array of month-names in swedish language.
$monthnames = array("","Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December");
$postdate = $monthnames[date("n", strtotime( $row['modify_date'] ))];
//Outputs something like '12 Februari'
Here is the prob, I want to check the $postdate variable and change it to "Today", "Yesterday" and "Day before yesterday" according to the date, how can I do so?
Thanks
If you're using timestamps to store dates in the database:
You can have preset intervals like:
$day = mktime(0,0,0,2,1,2001)-mktime(0,0,0,1,1,2001);
And you can compare the time now - timestamps/dates you have in the database with those intervals.
For example, let's say $dbTime contains a timestamp fetched with mysql:
$time = time() - $dbTime;
if($time<$day)
echo 'posted today';
elseif($time<($day*2))
echo 'posted yesterday';
etc etc.
Then you can use PHP's Date function to echo hrs, minutes, secs, or dates, if they are larger than your predefined intervals.
There is no such in-built function in PHP. You got to do calculations, the mktime function can be your friend in this case.
if (date of $timenow == date of $timepost) { today; }
else if (date of $timenow - 24h == date of $timepost) { yesterday; }
etc.
Be aware about summer/winter time change, timezones etc. so try to use mktime()
http://php.net/date
Hi I'm using php and sql through odbc to write a program and i hav got abit stuck in a part where i want to display the current date/time in the format date('Y-m-d H:i:s) but it only displays the gmt time. I want to add 8hours to it.Can any of you b able to help me.Thank you so much
Check out date_default_timezone_set. You can do something like:
date_default_timezone_set('America/Los_Angeles');
print 'Current datetime is: ' . date('Y-m-d H:i:s');
You could use that to set the timezone to whatever timezone you need time to be at, and then use date normally. Alternatively, you can do this, using strtotime:
print 'Current datetime is: ' date('Y-m-d H:i:s', strtotime('+8 hours'));
If you're looking for a way to display a timestamp in a user's local time, you can use JavaScript:
function showtime(t)
{
if (t == 0)
{
document.write("never");
return;
}
var currentTime = new Date(t);
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
document.write();
if (minutes < 10){
minutes = "0" + minutes;
}
if (seconds < 10){
seconds = "0" + seconds;
}
document.write(month + "/" + day + "/" + year + " " +
hours + ":" + minutes + ":" + seconds + " ");
if(hours > 11){
document.write("PM");
} else {
document.write("AM");
}
}
Then if you need to display a time, just make a call to it in the HTML and splice in the value from PHP:
<script type="text/javascript">showtime(<?=$time."000"?>)</script>
I would steer clear of the timezone method.
If i understood correctly, you want to add time, thus change it. An example could be, A task has been created NOW, and must be complete in 8 hours. The timezone method would only change the display of the date and time. Only change the timezone setting if you know your visitor's timezone, and datetime's must be shown relative to them.
Now: 1234418228 is 2009/02/12 00:57:08 in Montreal or 2009/02/11 09:57:08 in San Francisco. It's the exact same moment.
Appending to the first answer, date() and strtotime() are your friends.
strtotime( "+8 hours", $now )
$now being a timestamp of when it's supposed to relate to. So if your start time isn't time(), you can still use that. eg
strtotime( "+8 hours", strtotime( "2009/03/01 00:00:00" ); (8AM on 2009/03/01)
However, when dealing with intervals counted in weeks, or less, i prefer doing it 'mathematically'
$StartTime = strtotime( "2009/03/01 13:00:00" );
$EndTime = $StartTime + ( 8 * 60 * 60 );
date( "Y/m/d H:i:s", $EndTime ) ==> "2009/03/01 21:00:00"
3600 seconds in an hour, 86400 in a day.
You can't use this method for months, quarters or years because the number of seconds they last varies from one to the next.
If you want to use time for a certain timezone, then using date_default_timezone_set() is preferred. anyway you can provide the date() function another parmater: int timestamp. an integer representing the timestamp you would like date() to return the information about.
so if you would like to show date('Y-m-d H:i:s') for now you can use this:
$now = date('Y-m-d H:i:s', time() ); // time() returns current timestamp.
// if you omit the second parameter of date(), it will use current timestamp
// by default.
$_8hoursLater = date('Y-m-d H:i:s', time()+60*60*8 );
$_8hoursBefore = date('Y-m-d H:i:s', time()-60*60*8 );