Converting MySQL date to Java date - php

Say it I get a date from MySQL table like 2012-03-31.
I pass this date to a Java application. So Java needs that date as 2012, 02, 31.
Firstly I explode date, get second element, subtract one from the month value. Then I implode three elements and create new date string.
public function convertToJavaDate($mysqlDate) {
$pieces = explode("-",$mysqlDate);
return $pieces[0].", ".($pieces[1]-1).", ".$pieces[2];
}
Is there a quicker or smarter way to do this ?

For java see http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String, java.text.ParsePosition)

$date = strtotime('2012-03-31');
$javadata = date('Y, m, d', strtotime('-30 days', $date));

Related

Convert SQLite to MySQL datetime

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

Get previous weekday from sql date string in PHP

I have a date string in sql format (eg 2017-08-05) and will like to convert it to get the previous weekday.
I know that there is the strtotime method where I can get the previous weekday from a specific date, such as:
date("Y m d",strtotime("-2 Weekday", strtotime("2017-08-05")));
Is there a better (prettier) way to do this? Or is this the PHP way?
The same can be achieved with just one call to strtotime:
date("Y m d", strtotime("2017-08-05 -2 Weekday"))
By using Carbon
Carbon::parse('2017-08-05')->previousWeekday()
Or if you want sameway
Carbon::parse('2017-08-05')->subWeekday(2)->toDateString()
you can create a carbon
carbon::parse('2012-08-05')->previousWeekday();
No better way then this...
Try this:
// create a new instance of DateTimeImmutable
$date = new \DateTimeImmutable('2017-08-05');
// create a one-day interval
$interval = new \DateInterval('P1D');
// subtract the interval, and keep doing that while the day before is not a weekday
do {
$date = $date->sub($interval);
} while (5 < $date->format('N'));
echo $date->format('Y-m-d');
For reference, see:
http://php.net/manual/en/class.datetimeimmutable.php
http://php.net/manual/en/class.dateinterval.php
http://php.net/manual/en/function.date.php
For an example, see:
https://3v4l.org/aT6Zj

Store date directly in PHP variable

I want to store a specific date in a variable. If stored like $x="01/01/2016" it is acting as a string from which I cannot extract a part, like from getdate() year, month, day of the month, etc.
Use the DateTime object:
$dateTime = new DateTime('2016/01/01');
To get only parts of the date you can use the format method:
echo $dateTime->format('Y'); // it will display 2016
If you need to create it from the format you wrote in the question, then you can use the factory method createFromFormat:
$dateTime = DateTime::createFromFormat('d/m/Y', '01/01/2016');
echo $dateTime->format('Y/m/d');
This is work for me
$date = '20/May/2015:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);
this is what you are looking for http://php.net/manual/en/class.datetime.php
You can use $myDate = new DateTime('01/01/2016'); to declare date. To get year, month and date from the specified date, use echo $myDate->format('d m Y');
Change the format based on your need. To know more about date format refer

Dayname from 3 letter code

Currently I'm doing
$dayNames = array(
'Sun'=>'Sunday',
'Mon'=>'Monday',
'Tue'=>'Tuesday',
'Wed'=>'Wednesday',
'Thu'=>'Thursday',
'Fri'=>'Friday',
'Sat'=>'Saturday',
);
$weekday=$dayNames[$dayCode]; //where $dayCode is Sun, Mon...Sat
Is it the good way or there are any native PHP function for that ?
One possible approach:
$shortDay = 'Sun';
$fullDay = date('l', strtotime($shortDay)); // Sunday
Explanation: with strtotime, you essentially parse the given short dayname, constructing a new date based on it. With date('l') (it's lowercased L, not 1), you format it back into full textual representation of the day of the week.

Adjust a PHP date to the current year

I have a PHP date in a database, for example 8th August 2011. I have this date in a strtotime() format so I can display it as I please.
I need to adjust this date to make it 8th August 2013 (current year). What is the best way of doing this? So far, I've been racking my brains but to no avail.
Some of the answers you have so far have missed the point that you want to update any given date to the current year and have concentrated on turning 2011 into 2013, excluding the accepted answer. However, I feel that examples using the DateTime classes are always of use in these cases.
The accepted answer will result in a Notice:-
Notice: A non well formed numeric value encountered......
if your supplied date is the 29th February on a Leapyear, although it should still give the correct result.
Here is a generic function that will take any valid date and return the same date in the current year:-
/**
* #param String $dateString
* #return DateTime
*/
function updateDate($dateString){
$suppliedDate = new \DateTime($dateString);
$currentYear = (int)(new \DateTime())->format('Y');
return (new \DateTime())->setDate($currentYear, (int)$suppliedDate->format('m'), (int)$suppliedDate->format('d'));
}
For example:-
var_dump(updateDate('8th August 2011'));
See it working here and see the PHP manual for more information on the DateTime classes.
You don't say how you want to use the updated date, but DateTime is flexible enough to allow you to do with it as you wish. I would draw your attention to the DateTime::format() method as being particularly useful.
strtotime( date( 'd M ', $originaleDate ) . date( 'Y' ) );
This takes the day and month of the original time, adds the current year, and converts it to the new date.
You can also add the amount of seconds you want to add to the original timestamp. For 2 years this would be 63 113 852 seconds.
You could retrieve the timestamp of the same date two years later with strtotime() first parameter and then convert it in the format you want to display.
<?php
$date = "11/08/2011";
$time = strtotime($date);
$time_future = strtotime("+2 years", $time);
$future = date("d/m/Y", $time_future);
echo "NEW DATE : " . $future;
?>
You can for instance output it like this:
date('2013-m-d', strtotime($myTime))
Just like that... or use
$year = date('Y');
$myMonthDay = date('m-d', strtotime($myTime));
echo $year . '-' . $myMonthDay;
Use the date modify function Like this
$date = new DateTime('2011-08-08');
$date->modify('+2 years');
echo $date->format('Y-m-d') . "\n";
//will give "2013-08-08"

Categories