Date in a URL dd/mm/yyyy - php

I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01

When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

Related

dd/mm/YYYY format date - while loop problems adding 1 day

I am creating a while loop in PHP to iterate through all the days between two dates.
$startdate = "17/12/2022"; // my dd/mm/YYYY format start date
$startdate = str_replace('/', '-',$startdate); // convert the slashes to dashes to signify european date format
$startdate = strtotime($startdate); // convert the date into a date object
$enddate = "16/12/2023"; // my dd/mm/YYYY format start date
$enddate = str_replace('/', '-',$enddate); // convert the slashes to dashes to signify european date format
$enddate = strtotime($enddate); // convert the date into a date object
echo(date('d/m/Y',$startdate)."<br/>");
echo(date('d/m/Y',$enddate)."<br/>");
while($startdate <= $enddate) {
$dayofweek = date('w',$startdate); // a task
echo($dayofweek)."<br/>");
//I need to increment the $startdate by one day here?
}
?>
I believe that changing the slashes to dashes effectively instructs PHP to recognise the date as dd/mm/YYYY rather than mm/dd/YYYY. There are references (in many examples) to the PHP documentation outlining this functionality but the PHP documentation no longer shows this; nor does it show that it is depleted or has been changed. It is simply omitted. My tests show that it works but I cannot find reference to an alternative.
I then convert both dates to a date object using strtotime so that they can be compared.
I then need to add 1 day to the $startdate and loop, but it is the adding one day that is foxing me.
All the examples I can find are using todays date or a string not formatted dd/mm/YYYY.
I freely admit that I may have got the whole premise wrong but any pointers for good practise would be appreciated.
Using the DateTime class and the DateInterval Class and the DatePeriod class you get quite a bit of the heavy lifting done for you
$startdate = "17/12/2022"; // my dd/mm/YYYY format start date
$enddate = "31/12/2022"; // my dd/mm/YYYY format start date
$sDate = ( new Datetime() )->createFromFormat('d/m/Y', $startdate);
$eDate = ( new Datetime() )->createFromFormat('d/m/Y', $enddate);
$oneDay = new DateInterval('P1D');
$daterange = new DatePeriod($sDate, $oneDay ,$eDate);
foreach ( $daterange as $dat) {
echo $dat->format('d/m/y - w D') . PHP_EOL;
}
RESULTS using a smaller range of dates
17/12/22 - 6 Sat
18/12/22 - 0 Sun
19/12/22 - 1 Mon
20/12/22 - 2 Tue
21/12/22 - 3 Wed
22/12/22 - 4 Thu
23/12/22 - 5 Fri
24/12/22 - 6 Sat
25/12/22 - 0 Sun
26/12/22 - 1 Mon
27/12/22 - 2 Tue
28/12/22 - 3 Wed
29/12/22 - 4 Thu
30/12/22 - 5 Fri

Adding days to date, outputting incorrect dates? [duplicate]

I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01
When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

Date Format from common string

I have a date format but still on String like this :
$date = '24-01-2017' # (dd-mm-yy)
I want to display them into datetime in Indonesian format
Selasa, 24-01-2017
In english
Tuesday, 24-01-2017
So, I created like this :
$date_format = DateTime::createFromFormat(' j-M-Y', $date);
echo $date_format->date('d-M-Y')
Not works
You should be using the format() function.
$now = new DateTime();
echo $now->format('Y-m-d');
// or
$now = DateTime::createFromFormat('d-m-Y', $date);
echo $now->format('Y-m-d');
Change the format to whatever you please.
To get the day in your own language:
$days = array(
'0' => 'Mon day',
'1' => 'Tues - Day',
// ... etc
);
You can now do:
$day = $now->format('n');
echo $days[$day].', '.$now->format('d-m-Y');
From a website that I found by doing a search for 'Date Time in Indonesian format, php'
PHP : Displaying the date and time in indonesian language
By this time I hope you can create your own date and time using the timestamp
of the current date or your own timestamp.
I use Indonesia language and I want to
display the date in Indonesian language. How can I do it? That's a perfect
question and asked by most beginners.
Let's create an associative array of date and month in our own language.
$days = Array ("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu");
$months = Array (1=>"Januari", 2=>"Pebruari", 3=>"Maret", 4=>"April",
5=>"Mei", 6=>"Juni", 7=>"Juli", 8=>"Agustus", 9=>"September",
10=>"Oktober", 11=>"Nopember", 12=>"Desember");
After we create our own language of date and months, now it's time to display it.
print $days[date("w")]; // display name of day with our own language
print $months[date("n")];
From php.net :
F and M : A textual representation of a month, such as January or Sept January through December or Jan through Dec
m and n : Numeric representation of a month, with or without leading zeros 01 through 12 or 1 through 12
You have to use "m" and not "M"
BTW, the method is not "date" but "format". ;)
<?php
$date = '24-01-2017';
$date_format = DateTime::createFromFormat('j-m-Y', $date);
echo $date_format->format('D d-m-Y');
?>
This outputs :
Tue 24-01-2017

yyyy-mm-dd format with strtotime returning wrong result

$originalDate1 = '2013-12-30';
echo $newDate = date("o-m-d", strtotime($originalDate1));
Displaying 2014 instead of 2013
The problem is the "o"
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
You should use "Y" instead.
use Y-m-d instead of o-m-d
$originalDate1 = '2013-12-30';
echo $newDate = date("Y-m-d", strtotime($originalDate1)); // will output 2014-12-30

php convert a date to a timestamp

I'm trying to convert 2 dates to timestamps. One is generated by php (today's date) and the other is input by the user
$today = date("d-m-y"); // 01-12-13
$start_date = "28-11-13";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Problem is that the result are incorrect
Result:
01-12-13 > 28-11-13 1008201600 > 1857686400
what's wrong ?
Always use four-digit years, as using two-digit years leads to endless headaches. 13 is not treated as a year; instead, 01-12-13 and 28-11-13 are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".
If you use 2013 instead, the issues disappear:
$today = date("d-m-Y"); // 01-12-2013
$start_date = "28-11-2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Output:
01-12-2013 > 28-11-2013
1385884800 > 1385625600
My understanding of the documentation is that you are trying to convert a readable string into a Unix based timestamp. When you attempt to convert 01-12-13 and 28-11-13 you will get undesired results because the strtotime function has no recollection of how to properly interpret these values. Something along the lines of the code below should help you out.
$today = date("jS M Y"); // 1st Dec 2013
$start_date = "28th Nov 2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
output
1st Dec 2013 > 28th Nov 2013
1385877600 > 1385618400
Just adjust your code to fit within the limitations of the language and if these values are coming from a form field you should be able to quickly and easily convert them to a readable date.
The function assumes that you start with a year
(check : http://www.onlineconversion.com/unix_time.htm), change to
$start_date = "28-11-2013";
This way the function knows which is the year, it takes the default foramt which is (if not misstaken) Y-d-m

Categories