How do I convert php m.d.Y date to Unix timestamp? - php

I'm receiving date something like 11.30.2012 which I need to convert in UNIX timestamp but in PHP seems it's not working. How can I resolve this. Thanks in advance.

$date = '12.31.2000';
$timestamp = DateTime::createFromFormat('m.d.Y', $date)->getTimestamp();

try this
$date = "11.30.2012";
$x = explode(".",$date);
$req_date = $x[2]."-".$x[0]."-".$x[1];
$unixdate = strtotime($req_date);

try this
$date = "11.30.2012";
list($month, $day, $year) = split('.', $date);
$timeStamp = mktime(0, 0, 0, $month, $day, $year);

Related

Php change the order of the date

I am trying to display the date that I send by $_GET, my url format is for example http://127.0.0.1/index.php?date=01/08/2018.
The result is 08-01-2018 it change the order of the day and the month why ?
$orginal_date= $_GET['date'];
$date = date("d-m-Y", strtotime($original_date));
echo $date;
It automatically parses the date with format m/d/Y (american format). You could use the DateTime class to specify your format:
$date = DateTime::createFromFormat('d/m/Y', $_GET['date']);
echo $date->format('d-m-Y');
Documentation:
DateTime::createFromFormat
DateTime::format
list($month, $day, $year) = explode("/", $_GET["date"]);
$date = sprintf("%d-%02d-%02d", $year, $month, $day);
This looks like you may be assuming how php will use strtotime and convert to a timestamp. By default it thinks that 01/08/2018 is Jan 8 2018. There is a method date_create_from_format that you can tell it the format you are expecting and then you can format that to however you want it to look like.
$orig = $_GET['date'];
$date = date_create_from_format('d/m/Y', $orig);
echo date_format($date, 'd-m-Y');
//result is 01-08-2018
Try This
list($month, $day, $year) = explode("/", $_GET["date"]);
echo date('d-m-Y',mktime(0, 0, 0, $month,$day, $year));

work with jmktime in JDF library

I have a problem with converting Jalali date to timestamp. For do that i'm using JDF library. So far I've Done this:
$jalalidate = $this->input->post('c_adddate');
list($year, $month, $day) = explode('/', $jalalidate);
$timestamp = jmktime(0, 0, 0, $month, $day, $year);
But it always show the current time!
echo date("Y-m-d", $timestamp);
Just Shows the current time.

Retrieve date, month, year from a date in php

I have this piece of code which gets me a field from the database:
$end_date=$row1['end_date'];
If i print it it gives me something like: 25-09-2012
What i need is to get the month value, the year and date.
something like:
$month=09;
$day=25;
$year=2012;
How can i do that?
thanks!
Using DateTime:
$date = new DateTime($row1['end_date']);
$year = $date -> format('Y');
$month = $date -> format('m');
$day = $date -> format('d');
If your timestamps are all like the one provided, keep it simple:
list($day, $month, $year) = explode('-', $row1['end_date']);
In your case, you can use the explode function like this :
// store a string containing "25-09-2012"
$end_date = $row1['end_date'];
// split "25-09-2012" into an array of three elements
$thedate = explode("-", $end_date);
// retrieve the values
$month = $thedate[0]; // 25
$day = $thedate[1]; // 09
$year = $thedate[2]; // 2012
try
[month('end_date')]
[day('end_date')]
[year('end_date')]
Or use explode and use - as the delimiter
Take a peak at this helpful tutorial describing various formatting methods and useful date functions in PHP:
Date/Time Functions
Date Formats
A. You can use DateTime
$date = DateTime::createFromFormat('d-m-Y',$row1['end_date']);
$month = $date->format("m");
$day = $date->format("d");
$year = $date->format("Y");
B. Using strtotime
$date = strtotime($row1['end_date']);
$month = date("m", $date);
$day = date("d", $date);
$year = date("Y", $date);
C. You can just sscanf scan through the string
$date = sscanf($row1['end_date'], "%d-%d-%d");
$month = $date[0] ;
$day = $date[1] ;
$year = $date[2] ;
D. Another method is using list & explode
list($day, $month, $year) = explode('-', $row1['end_date']);
Do it on a single line and format it however you would like. (Dec, December, 12) and so on with date().
list($month, $day, $year) = explode('-', date('m-d-Y', strtotime($row1['end_date'])));
$values = getdate(strtotime($row1['end_date']));
echo $values['mon']; //month
echo $values['mday']; //day
echo $values['year']; //year

PHP split string into parts

I have string with following format:
$date = "2012-07-22 17:48:24";
I want to get the year, month and date in the variables and ignore the time. I am trying following:
list($year, $month, $day) = split('[-]', $date);
This returns correct values to $year and $month, but the $day gets: "22 17:48:24", while I want to get only 22.
Instead of exploding the value you could use a DateTime object:
<?php
$date = "2012-07-22 17:48:24";
$dateTime = new DateTime($date);
var_dump(array(
'year' => $dateTime->format('Y'),
'month' => $dateTime->format('m'),
'day' => $dateTime->format('d'),
));
This would be the most flexible option imho.
As #zerkms noted in his comment you could also use strtotime() and date(), but I find myself only using the DateTime class lately. Not only because it has a nice OO API, but also because it will keep on working after the year 2038 :-). The comment is not wrong though.
There's also the sscanf() function.
sscanf('2012-07-22 17:48:24', '%d-%d-%d', $year, $month, $day);
Use explode:
$date = "2012-07-22 17:48:24";
$date = explode(" ", $date);
list($year, $month, $day) = split('[-]', $date[0]);
EDIT:
You should use explode for the date too:
list($year, $month, $day) = explode('-', $date[0]);
The use of split is discouraged as it was deprecated.
$date = "2012-07-22 17:48:24";
preg_match('~^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$~', $date, $m);
print_r($m);
$date = date('Y-m-d', strtoime("2012-07-22 17:48:24"));
list($year, $month, $day) = split('[-]', $date);

conflict between implode function and strtotime

I have a problem in this code:
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
function dateImplodeFunction($year, $month, $day){
$array = array($year, $month, $day);
$date = date('Y-m-d', strtotime( implode("-", $array)));
return $date;
}
based on the code above I'm going to create a function where there are 3 inputs month, day and a year. When I input those 3 these variables will passed to this function, combine those 3 variables and use the implode function to create a format based on what date you specified. for instance let's say if I input 10/01/1989 it will echo the display 10/01/1989.
also I need to use date function together with strtotime function (refer to the code above) for database, setting my date field into date data type.
The problem here is if I input 10-01-1989, it returns/dipslays the value of 01-01-1970 why??
I figured it out that there is a conflict between strtotime and implode function due to test. I've search through google but it find none. I hope you can help me. Thanks in advance.
sorry for the bad english =P
I would just use the Date Time class.
$month = '07';
$day = '26';
$year = '2012';
$timezone = new DateTimeZone('America/New_York');
$date = new DateTime( "{$year}-{$month}-{$day}", $timezone );
print $date->format('Y-m-d H:i:s');
# Output: 2012-07-26 00:00:00
http://codepad.org/VxwDHPeU
You should use mktime :
function dateImplodeFunction($year, $month, $day)
{
return date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
}
Documentation of mktime here

Categories