work with jmktime in JDF library - php

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.

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));

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

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);

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);

convert string to date type in php

I have 3 variable $day , $month and $year and I want to create a date variable with these 3 variables in this fromat : yyyy-mm-dd in php.
$year variable is a persian year for example $year= 1390
this code works properly but not for persian date :
date("d-m-Y", mktime(0, 0, 0, $fMonth, $fDay, $fYear));
How I can do it ?
http://ir.php.net/manual/en/function.mktime.php
Usage:
$myTime = date("d/m/Y", mktime(0, 0, 0, $month, $day, $year));
So you'd want something like this:
date("d-M-Y", mktime(0, 0, 0, $month, $day, $year));
EDIT: Oops, sorry, fixed.
I'm not sure what you mean by date variable but. You can try these.
mktime()
mktime(0, 0, 0, $date, $month, $year)
or you can create a DateTime Object
$date = new DateTime('2000-01-01');
or you can try strtotime() if you have strings in your variables.

Can't get specific date from db -php

I'm trying to a date timestampted in my mysql db, but my code shows the current date instead.
This is the code that I'm trying:
$db_date = $row_news['nm_date'];
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 4, 2);
$day = substr($db_date, 6, 2);
$orgdate = date("l dS F Y",mktime($mon, $day, $year));
$date = $orgdate
The script is meant to email $date, which should be the value of $row_news['nm_date'], but instead I get the current date "Saturday 11th September 2010.
Thanks for your help.
You're getting today's date because mktime() is not returning a valid value. If you check the manual entry for mktime() - http://www.php.net/mktime - you'll see that the parameter order is:
mktime ($hour, $minute, $second, $month, $day, $year)
so you probably want:
$orgdate = date("l dS F Y", mktime(12, 0, 0, $mon, $day, $year));
your code assumes that the date is in YYYYMMDD format (or YYYYMMDDHHIISS). Assuming that is correct, and it's not actually in date format (YYYY-MM-DD) then the above should fix your problem.
Edit: If the dates are in YYYY-MM-DD format you need to adjust your substrings to allow for the dashes:
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 5, 2);
$day = substr($db_date, 8, 2);
$db_date = $row_news['nm_date'];
$orgdate = date("l dS F Y", $db_date);
$date = $orgdate;

Categories