I want to convert the string 1433669892 to 07-06-2015 (June, 7th, 2015).
For that, I'm using the code:
$date = date('d-m-Y',strtotime ($row['order_created']));
Where:
$row['order_created']= '1433669892'
Which is returning:
31-12-1969
Totally different of what I expected. I know this should be trivial, but I can't manage to get the right date.
Try this:
$date = date('d-m-Y', intval( $row['order_created'] ) );
The timestamp passed to the function date() should be an integer as you seem to have in your row.
Edit Casting to an integer
Related
I am using DateTime function of php. I get a date from a calendar in format d-m-Y and pass it via ajax to my function. I am getting the date right till this step.
When I try to store the date in unix format using:
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y', $data['date']);
$final_date=$ai_ff_date->format('U');
The date stored is wrong. Suppose the date I passed via ajax is 26-12-2016 then in database 27-12-2016 is stored. Why its counting one more day then the input.
use this code :
$date = date('Y-m-d H:i:s', strtotime('-1 day', $stop_date));
$ai_ff_date=DateTime::CreateFromFormat('d-m-Y',$date);
$final_date=$ai_ff_date->format('U');
and please check the variable (code not tested)
You might want to convert the Date-Format to "Y-m-d" First and then call-in the DateTime() Constructor. However, since what you are trying to do is just get the TimeStamp you might also do that directly without using DateTime. The Snippet below shows what is meant here:
<?php
$data = ['date'=>"13-12-2016"]; //<== JUST AN EXAMPLE FOR TESTING!!!
// SIMPLY CONVERT THE DATE TO Y-m-d FIRST.
$dateYMD = date("Y-m-d", strtotime($data['date']));
// THEN USE DateTime CONSTRUCTOR TO CREATE A NEW DateTime INSTANCE
// AND THEN RUN THE FORMAT YOU WISH::
$final_date = (new DateTime($dateYMD))->format('U');
var_dump($final_date); //<== YIELDS: string '1481583600' (length=10)
var_dump(date("Y-m-d", $final_date)); //<== YIELDS: string '2016-12-13' (length=10)
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 have an mysql database with a date column and i am using datatype datetime.
But now i want change my datatype from datetime to long integer
I would like to know how to convert date to any integer value.
Let say i have a date
i.e 2012-03-27 18:47:00
so I was wondering if it's possible to convert into any integer number like 131221154
Use strtotime function of PHP.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
echo strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
And to make it back again, just use the date function of PHP:
$long = strtotime('2012-03-27 18:47:00'); //--> which results to 1332866820
echo date('Y-m-d H:i:s', $long);
check this
<?php
$timestamp = strtotime('1st January 2004'); //1072915200
// this prints the year in a two digit format
// however, as this would start with a "0", it
// only prints "4"
echo idate('y', $timestamp);
?>
I need to compare date/time that I get from MySQL with a format like: 2013-05-17 15:07:29
From another database, I have data and time separated and in the notation: 130998 081836
I have concatenated the two strings to get only one and I'm trying to convert it to my desired format using:
$dateTimeNmea = $array[9]." ".$array[1]; // 130998 081836
$dateTime = date("Y-m-d H:i:s", $dateTimeNmea); // 1970-01-02 13:23:18
So "it works" on the format but the values are wrong. It could be 1998-09-13 08:18:36
Where is my fault?
It has format siH dmy. Try date_parse_from_format('siH dmy', $string) to get it in array.
130998 081836 is not a format for a date that the date function can understand.
For starters the date() function expects the second param to be a timestamp (read docs for it here)
Then you would need to parse the string into a useable date format via date_parse_from_format and finally into a timestamp
Something like
$string = '130998 081836';
$date = date_parse_from_format('dmY His', $string);
$dateString = date('Y-m-d H:i:s', strtotime(vsprintf('%s-%s-%s %s:%s:%s', $date)));
var_dump($dateString); // var dump just for output/test
The strtotime(vsprintf('%s-%s-%s %s:%s:%s', $date) formats your parsed date into a timestamp which can then be used in date methods second param to get exactly the format you need.
I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);